drm_prime.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 <drm/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. Prime will do this automatically for drivers that
  55. * use the drm_gem_prime_{import,export} helpers.
  56. */
  57. struct drm_prime_member {
  58. struct list_head entry;
  59. struct dma_buf *dma_buf;
  60. uint32_t handle;
  61. };
  62. static struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
  63. enum dma_data_direction dir)
  64. {
  65. struct drm_gem_object *obj = attach->dmabuf->priv;
  66. struct sg_table *sgt;
  67. mutex_lock(&obj->dev->struct_mutex);
  68. sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
  69. if (!IS_ERR_OR_NULL(sgt))
  70. dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir);
  71. mutex_unlock(&obj->dev->struct_mutex);
  72. return sgt;
  73. }
  74. static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
  75. struct sg_table *sgt, enum dma_data_direction dir)
  76. {
  77. dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
  78. sg_free_table(sgt);
  79. kfree(sgt);
  80. }
  81. static void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
  82. {
  83. struct drm_gem_object *obj = dma_buf->priv;
  84. if (obj->export_dma_buf == dma_buf) {
  85. /* drop the reference on the export fd holds */
  86. obj->export_dma_buf = NULL;
  87. drm_gem_object_unreference_unlocked(obj);
  88. }
  89. }
  90. static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
  91. {
  92. struct drm_gem_object *obj = dma_buf->priv;
  93. struct drm_device *dev = obj->dev;
  94. return dev->driver->gem_prime_vmap(obj);
  95. }
  96. static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
  97. {
  98. struct drm_gem_object *obj = dma_buf->priv;
  99. struct drm_device *dev = obj->dev;
  100. dev->driver->gem_prime_vunmap(obj, vaddr);
  101. }
  102. static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
  103. unsigned long page_num)
  104. {
  105. return NULL;
  106. }
  107. static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
  108. unsigned long page_num, void *addr)
  109. {
  110. }
  111. static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
  112. unsigned long page_num)
  113. {
  114. return NULL;
  115. }
  116. static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
  117. unsigned long page_num, void *addr)
  118. {
  119. }
  120. static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
  121. struct vm_area_struct *vma)
  122. {
  123. return -EINVAL;
  124. }
  125. static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
  126. .map_dma_buf = drm_gem_map_dma_buf,
  127. .unmap_dma_buf = drm_gem_unmap_dma_buf,
  128. .release = drm_gem_dmabuf_release,
  129. .kmap = drm_gem_dmabuf_kmap,
  130. .kmap_atomic = drm_gem_dmabuf_kmap_atomic,
  131. .kunmap = drm_gem_dmabuf_kunmap,
  132. .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
  133. .mmap = drm_gem_dmabuf_mmap,
  134. .vmap = drm_gem_dmabuf_vmap,
  135. .vunmap = drm_gem_dmabuf_vunmap,
  136. };
  137. /**
  138. * DOC: PRIME Helpers
  139. *
  140. * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
  141. * simpler APIs by using the helper functions @drm_gem_prime_export and
  142. * @drm_gem_prime_import. These functions implement dma-buf support in terms of
  143. * five lower-level driver callbacks:
  144. *
  145. * Export callbacks:
  146. *
  147. * - @gem_prime_pin (optional): prepare a GEM object for exporting
  148. *
  149. * - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
  150. *
  151. * - @gem_prime_vmap: vmap a buffer exported by your driver
  152. *
  153. * - @gem_prime_vunmap: vunmap a buffer exported by your driver
  154. *
  155. * Import callback:
  156. *
  157. * - @gem_prime_import_sg_table (import): produce a GEM object from another
  158. * driver's scatter/gather table
  159. */
  160. struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
  161. struct drm_gem_object *obj, int flags)
  162. {
  163. if (dev->driver->gem_prime_pin) {
  164. int ret = dev->driver->gem_prime_pin(obj);
  165. if (ret)
  166. return ERR_PTR(ret);
  167. }
  168. return dma_buf_export(obj, &drm_gem_prime_dmabuf_ops, obj->size,
  169. 0600);
  170. }
  171. EXPORT_SYMBOL(drm_gem_prime_export);
  172. int drm_gem_prime_handle_to_fd(struct drm_device *dev,
  173. struct drm_file *file_priv, uint32_t handle, uint32_t flags,
  174. int *prime_fd)
  175. {
  176. struct drm_gem_object *obj;
  177. void *buf;
  178. int ret;
  179. obj = drm_gem_object_lookup(dev, file_priv, handle);
  180. if (!obj)
  181. return -ENOENT;
  182. mutex_lock(&file_priv->prime.lock);
  183. /* re-export the original imported object */
  184. if (obj->import_attach) {
  185. get_dma_buf(obj->import_attach->dmabuf);
  186. *prime_fd = dma_buf_fd(obj->import_attach->dmabuf, flags);
  187. drm_gem_object_unreference_unlocked(obj);
  188. mutex_unlock(&file_priv->prime.lock);
  189. return 0;
  190. }
  191. if (obj->export_dma_buf) {
  192. get_dma_buf(obj->export_dma_buf);
  193. *prime_fd = dma_buf_fd(obj->export_dma_buf, flags);
  194. drm_gem_object_unreference_unlocked(obj);
  195. } else {
  196. buf = dev->driver->gem_prime_export(dev, obj, flags);
  197. if (IS_ERR(buf)) {
  198. /* normally the created dma-buf takes ownership of the ref,
  199. * but if that fails then drop the ref
  200. */
  201. drm_gem_object_unreference_unlocked(obj);
  202. mutex_unlock(&file_priv->prime.lock);
  203. return PTR_ERR(buf);
  204. }
  205. obj->export_dma_buf = buf;
  206. *prime_fd = dma_buf_fd(buf, flags);
  207. }
  208. /* if we've exported this buffer the cheat and add it to the import list
  209. * so we get the correct handle back
  210. */
  211. ret = drm_prime_add_imported_buf_handle(&file_priv->prime,
  212. obj->export_dma_buf, handle);
  213. if (ret) {
  214. drm_gem_object_unreference_unlocked(obj);
  215. mutex_unlock(&file_priv->prime.lock);
  216. return ret;
  217. }
  218. mutex_unlock(&file_priv->prime.lock);
  219. return 0;
  220. }
  221. EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
  222. struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
  223. struct dma_buf *dma_buf)
  224. {
  225. struct dma_buf_attachment *attach;
  226. struct sg_table *sgt;
  227. struct drm_gem_object *obj;
  228. int ret;
  229. if (!dev->driver->gem_prime_import_sg_table)
  230. return ERR_PTR(-EINVAL);
  231. if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
  232. obj = dma_buf->priv;
  233. if (obj->dev == dev) {
  234. /*
  235. * Importing dmabuf exported from out own gem increases
  236. * refcount on gem itself instead of f_count of dmabuf.
  237. */
  238. drm_gem_object_reference(obj);
  239. dma_buf_put(dma_buf);
  240. return obj;
  241. }
  242. }
  243. attach = dma_buf_attach(dma_buf, dev->dev);
  244. if (IS_ERR(attach))
  245. return ERR_PTR(PTR_ERR(attach));
  246. sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  247. if (IS_ERR_OR_NULL(sgt)) {
  248. ret = PTR_ERR(sgt);
  249. goto fail_detach;
  250. }
  251. obj = dev->driver->gem_prime_import_sg_table(dev, dma_buf->size, sgt);
  252. if (IS_ERR(obj)) {
  253. ret = PTR_ERR(obj);
  254. goto fail_unmap;
  255. }
  256. obj->import_attach = attach;
  257. return obj;
  258. fail_unmap:
  259. dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
  260. fail_detach:
  261. dma_buf_detach(dma_buf, attach);
  262. return ERR_PTR(ret);
  263. }
  264. EXPORT_SYMBOL(drm_gem_prime_import);
  265. int drm_gem_prime_fd_to_handle(struct drm_device *dev,
  266. struct drm_file *file_priv, int prime_fd, uint32_t *handle)
  267. {
  268. struct dma_buf *dma_buf;
  269. struct drm_gem_object *obj;
  270. int ret;
  271. dma_buf = dma_buf_get(prime_fd);
  272. if (IS_ERR(dma_buf))
  273. return PTR_ERR(dma_buf);
  274. mutex_lock(&file_priv->prime.lock);
  275. ret = drm_prime_lookup_imported_buf_handle(&file_priv->prime,
  276. dma_buf, handle);
  277. if (!ret) {
  278. ret = 0;
  279. goto out_put;
  280. }
  281. /* never seen this one, need to import */
  282. obj = dev->driver->gem_prime_import(dev, dma_buf);
  283. if (IS_ERR(obj)) {
  284. ret = PTR_ERR(obj);
  285. goto out_put;
  286. }
  287. ret = drm_gem_handle_create(file_priv, obj, handle);
  288. drm_gem_object_unreference_unlocked(obj);
  289. if (ret)
  290. goto out_put;
  291. ret = drm_prime_add_imported_buf_handle(&file_priv->prime,
  292. dma_buf, *handle);
  293. if (ret)
  294. goto fail;
  295. mutex_unlock(&file_priv->prime.lock);
  296. return 0;
  297. fail:
  298. /* hmm, if driver attached, we are relying on the free-object path
  299. * to detach.. which seems ok..
  300. */
  301. drm_gem_object_handle_unreference_unlocked(obj);
  302. out_put:
  303. dma_buf_put(dma_buf);
  304. mutex_unlock(&file_priv->prime.lock);
  305. return ret;
  306. }
  307. EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
  308. int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
  309. struct drm_file *file_priv)
  310. {
  311. struct drm_prime_handle *args = data;
  312. uint32_t flags;
  313. if (!drm_core_check_feature(dev, DRIVER_PRIME))
  314. return -EINVAL;
  315. if (!dev->driver->prime_handle_to_fd)
  316. return -ENOSYS;
  317. /* check flags are valid */
  318. if (args->flags & ~DRM_CLOEXEC)
  319. return -EINVAL;
  320. /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
  321. flags = args->flags & DRM_CLOEXEC;
  322. return dev->driver->prime_handle_to_fd(dev, file_priv,
  323. args->handle, flags, &args->fd);
  324. }
  325. int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
  326. struct drm_file *file_priv)
  327. {
  328. struct drm_prime_handle *args = data;
  329. if (!drm_core_check_feature(dev, DRIVER_PRIME))
  330. return -EINVAL;
  331. if (!dev->driver->prime_fd_to_handle)
  332. return -ENOSYS;
  333. return dev->driver->prime_fd_to_handle(dev, file_priv,
  334. args->fd, &args->handle);
  335. }
  336. /*
  337. * drm_prime_pages_to_sg
  338. *
  339. * this helper creates an sg table object from a set of pages
  340. * the driver is responsible for mapping the pages into the
  341. * importers address space
  342. */
  343. struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages)
  344. {
  345. struct sg_table *sg = NULL;
  346. struct scatterlist *iter;
  347. int i;
  348. int ret;
  349. sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
  350. if (!sg)
  351. goto out;
  352. ret = sg_alloc_table(sg, nr_pages, GFP_KERNEL);
  353. if (ret)
  354. goto out;
  355. for_each_sg(sg->sgl, iter, nr_pages, i)
  356. sg_set_page(iter, pages[i], PAGE_SIZE, 0);
  357. return sg;
  358. out:
  359. kfree(sg);
  360. return NULL;
  361. }
  362. EXPORT_SYMBOL(drm_prime_pages_to_sg);
  363. /* export an sg table into an array of pages and addresses
  364. this is currently required by the TTM driver in order to do correct fault
  365. handling */
  366. int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
  367. dma_addr_t *addrs, int max_pages)
  368. {
  369. unsigned count;
  370. struct scatterlist *sg;
  371. struct page *page;
  372. u32 len, offset;
  373. int pg_index;
  374. dma_addr_t addr;
  375. pg_index = 0;
  376. for_each_sg(sgt->sgl, sg, sgt->nents, count) {
  377. len = sg->length;
  378. offset = sg->offset;
  379. page = sg_page(sg);
  380. addr = sg_dma_address(sg);
  381. while (len > 0) {
  382. if (WARN_ON(pg_index >= max_pages))
  383. return -1;
  384. pages[pg_index] = page;
  385. if (addrs)
  386. addrs[pg_index] = addr;
  387. page++;
  388. addr += PAGE_SIZE;
  389. len -= PAGE_SIZE;
  390. pg_index++;
  391. }
  392. }
  393. return 0;
  394. }
  395. EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
  396. /* helper function to cleanup a GEM/prime object */
  397. void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
  398. {
  399. struct dma_buf_attachment *attach;
  400. struct dma_buf *dma_buf;
  401. attach = obj->import_attach;
  402. if (sg)
  403. dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
  404. dma_buf = attach->dmabuf;
  405. dma_buf_detach(attach->dmabuf, attach);
  406. /* remove the reference */
  407. dma_buf_put(dma_buf);
  408. }
  409. EXPORT_SYMBOL(drm_prime_gem_destroy);
  410. void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
  411. {
  412. INIT_LIST_HEAD(&prime_fpriv->head);
  413. mutex_init(&prime_fpriv->lock);
  414. }
  415. EXPORT_SYMBOL(drm_prime_init_file_private);
  416. void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
  417. {
  418. struct drm_prime_member *member, *safe;
  419. list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
  420. list_del(&member->entry);
  421. kfree(member);
  422. }
  423. }
  424. EXPORT_SYMBOL(drm_prime_destroy_file_private);
  425. int drm_prime_add_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle)
  426. {
  427. struct drm_prime_member *member;
  428. member = kmalloc(sizeof(*member), GFP_KERNEL);
  429. if (!member)
  430. return -ENOMEM;
  431. member->dma_buf = dma_buf;
  432. member->handle = handle;
  433. list_add(&member->entry, &prime_fpriv->head);
  434. return 0;
  435. }
  436. EXPORT_SYMBOL(drm_prime_add_imported_buf_handle);
  437. int drm_prime_lookup_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle)
  438. {
  439. struct drm_prime_member *member;
  440. list_for_each_entry(member, &prime_fpriv->head, entry) {
  441. if (member->dma_buf == dma_buf) {
  442. *handle = member->handle;
  443. return 0;
  444. }
  445. }
  446. return -ENOENT;
  447. }
  448. EXPORT_SYMBOL(drm_prime_lookup_imported_buf_handle);
  449. void drm_prime_remove_imported_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf)
  450. {
  451. struct drm_prime_member *member, *safe;
  452. mutex_lock(&prime_fpriv->lock);
  453. list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
  454. if (member->dma_buf == dma_buf) {
  455. list_del(&member->entry);
  456. kfree(member);
  457. }
  458. }
  459. mutex_unlock(&prime_fpriv->lock);
  460. }
  461. EXPORT_SYMBOL(drm_prime_remove_imported_buf_handle);