drm_prime.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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 <linux/rbtree.h>
  31. #include <drm/drm_prime.h>
  32. #include <drm/drm_gem.h>
  33. #include <drm/drmP.h>
  34. #include "drm_internal.h"
  35. /*
  36. * DMA-BUF/GEM Object references and lifetime overview:
  37. *
  38. * On the export the dma_buf holds a reference to the exporting GEM
  39. * object. It takes this reference in handle_to_fd_ioctl, when it
  40. * first calls .prime_export and stores the exporting GEM object in
  41. * the dma_buf priv. This reference needs to be released when the
  42. * final reference to the &dma_buf itself is dropped and its
  43. * &dma_buf_ops.release function is called. For GEM-based drivers,
  44. * the dma_buf should be exported using drm_gem_dmabuf_export() and
  45. * then released by drm_gem_dmabuf_release().
  46. *
  47. * On the import the importing GEM object holds a reference to the
  48. * dma_buf (which in turn holds a ref to the exporting GEM object).
  49. * It takes that reference in the fd_to_handle ioctl.
  50. * It calls dma_buf_get, creates an attachment to it and stores the
  51. * attachment in the GEM object. When this attachment is destroyed
  52. * when the imported object is destroyed, we remove the attachment
  53. * and drop the reference to the dma_buf.
  54. *
  55. * When all the references to the &dma_buf are dropped, i.e. when
  56. * userspace has closed both handles to the imported GEM object (through the
  57. * FD_TO_HANDLE IOCTL) and closed the file descriptor of the exported
  58. * (through the HANDLE_TO_FD IOCTL) dma_buf, and all kernel-internal references
  59. * are also gone, then the dma_buf gets destroyed. This can also happen as a
  60. * part of the clean up procedure in the drm_release() function if userspace
  61. * fails to properly clean up. Note that both the kernel and userspace (by
  62. * keeeping the PRIME file descriptors open) can hold references onto a
  63. * &dma_buf.
  64. *
  65. * Thus the chain of references always flows in one direction
  66. * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
  67. *
  68. * Self-importing: if userspace is using PRIME as a replacement for flink
  69. * then it will get a fd->handle request for a GEM object that it created.
  70. * Drivers should detect this situation and return back the gem object
  71. * from the dma-buf private. Prime will do this automatically for drivers that
  72. * use the drm_gem_prime_{import,export} helpers.
  73. *
  74. * GEM struct &dma_buf_ops symbols are now exported. They can be resued by
  75. * drivers which implement GEM interface.
  76. */
  77. struct drm_prime_member {
  78. struct dma_buf *dma_buf;
  79. uint32_t handle;
  80. struct rb_node dmabuf_rb;
  81. struct rb_node handle_rb;
  82. };
  83. struct drm_prime_attachment {
  84. struct sg_table *sgt;
  85. enum dma_data_direction dir;
  86. };
  87. static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
  88. struct dma_buf *dma_buf, uint32_t handle)
  89. {
  90. struct drm_prime_member *member;
  91. struct rb_node **p, *rb;
  92. member = kmalloc(sizeof(*member), GFP_KERNEL);
  93. if (!member)
  94. return -ENOMEM;
  95. get_dma_buf(dma_buf);
  96. member->dma_buf = dma_buf;
  97. member->handle = handle;
  98. rb = NULL;
  99. p = &prime_fpriv->dmabufs.rb_node;
  100. while (*p) {
  101. struct drm_prime_member *pos;
  102. rb = *p;
  103. pos = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
  104. if (dma_buf > pos->dma_buf)
  105. p = &rb->rb_right;
  106. else
  107. p = &rb->rb_left;
  108. }
  109. rb_link_node(&member->dmabuf_rb, rb, p);
  110. rb_insert_color(&member->dmabuf_rb, &prime_fpriv->dmabufs);
  111. rb = NULL;
  112. p = &prime_fpriv->handles.rb_node;
  113. while (*p) {
  114. struct drm_prime_member *pos;
  115. rb = *p;
  116. pos = rb_entry(rb, struct drm_prime_member, handle_rb);
  117. if (handle > pos->handle)
  118. p = &rb->rb_right;
  119. else
  120. p = &rb->rb_left;
  121. }
  122. rb_link_node(&member->handle_rb, rb, p);
  123. rb_insert_color(&member->handle_rb, &prime_fpriv->handles);
  124. return 0;
  125. }
  126. static struct dma_buf *drm_prime_lookup_buf_by_handle(struct drm_prime_file_private *prime_fpriv,
  127. uint32_t handle)
  128. {
  129. struct rb_node *rb;
  130. rb = prime_fpriv->handles.rb_node;
  131. while (rb) {
  132. struct drm_prime_member *member;
  133. member = rb_entry(rb, struct drm_prime_member, handle_rb);
  134. if (member->handle == handle)
  135. return member->dma_buf;
  136. else if (member->handle < handle)
  137. rb = rb->rb_right;
  138. else
  139. rb = rb->rb_left;
  140. }
  141. return NULL;
  142. }
  143. static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv,
  144. struct dma_buf *dma_buf,
  145. uint32_t *handle)
  146. {
  147. struct rb_node *rb;
  148. rb = prime_fpriv->dmabufs.rb_node;
  149. while (rb) {
  150. struct drm_prime_member *member;
  151. member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
  152. if (member->dma_buf == dma_buf) {
  153. *handle = member->handle;
  154. return 0;
  155. } else if (member->dma_buf < dma_buf) {
  156. rb = rb->rb_right;
  157. } else {
  158. rb = rb->rb_left;
  159. }
  160. }
  161. return -ENOENT;
  162. }
  163. /**
  164. * drm_gem_map_attach - dma_buf attach implementation for GEM
  165. * @dma_buf: buffer to attach device to
  166. * @target_dev: not used
  167. * @attach: buffer attachment data
  168. *
  169. * Allocates &drm_prime_attachment and calls &drm_driver.gem_prime_pin for
  170. * device specific attachment. This can be used as the &dma_buf_ops.attach
  171. * callback.
  172. *
  173. * Returns 0 on success, negative error code on failure.
  174. */
  175. int drm_gem_map_attach(struct dma_buf *dma_buf, struct device *target_dev,
  176. struct dma_buf_attachment *attach)
  177. {
  178. struct drm_prime_attachment *prime_attach;
  179. struct drm_gem_object *obj = dma_buf->priv;
  180. struct drm_device *dev = obj->dev;
  181. prime_attach = kzalloc(sizeof(*prime_attach), GFP_KERNEL);
  182. if (!prime_attach)
  183. return -ENOMEM;
  184. prime_attach->dir = DMA_NONE;
  185. attach->priv = prime_attach;
  186. if (!dev->driver->gem_prime_pin)
  187. return 0;
  188. return dev->driver->gem_prime_pin(obj);
  189. }
  190. EXPORT_SYMBOL(drm_gem_map_attach);
  191. /**
  192. * drm_gem_map_detach - dma_buf detach implementation for GEM
  193. * @dma_buf: buffer to detach from
  194. * @attach: attachment to be detached
  195. *
  196. * Cleans up &dma_buf_attachment. This can be used as the &dma_buf_ops.detach
  197. * callback.
  198. */
  199. void drm_gem_map_detach(struct dma_buf *dma_buf,
  200. struct dma_buf_attachment *attach)
  201. {
  202. struct drm_prime_attachment *prime_attach = attach->priv;
  203. struct drm_gem_object *obj = dma_buf->priv;
  204. struct drm_device *dev = obj->dev;
  205. if (prime_attach) {
  206. struct sg_table *sgt = prime_attach->sgt;
  207. if (sgt) {
  208. if (prime_attach->dir != DMA_NONE)
  209. dma_unmap_sg_attrs(attach->dev, sgt->sgl,
  210. sgt->nents,
  211. prime_attach->dir,
  212. DMA_ATTR_SKIP_CPU_SYNC);
  213. sg_free_table(sgt);
  214. }
  215. kfree(sgt);
  216. kfree(prime_attach);
  217. attach->priv = NULL;
  218. }
  219. if (dev->driver->gem_prime_unpin)
  220. dev->driver->gem_prime_unpin(obj);
  221. }
  222. EXPORT_SYMBOL(drm_gem_map_detach);
  223. void drm_prime_remove_buf_handle_locked(struct drm_prime_file_private *prime_fpriv,
  224. struct dma_buf *dma_buf)
  225. {
  226. struct rb_node *rb;
  227. rb = prime_fpriv->dmabufs.rb_node;
  228. while (rb) {
  229. struct drm_prime_member *member;
  230. member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
  231. if (member->dma_buf == dma_buf) {
  232. rb_erase(&member->handle_rb, &prime_fpriv->handles);
  233. rb_erase(&member->dmabuf_rb, &prime_fpriv->dmabufs);
  234. dma_buf_put(dma_buf);
  235. kfree(member);
  236. return;
  237. } else if (member->dma_buf < dma_buf) {
  238. rb = rb->rb_right;
  239. } else {
  240. rb = rb->rb_left;
  241. }
  242. }
  243. }
  244. /**
  245. * drm_gem_map_dma_buf - map_dma_buf implementation for GEM
  246. * @attach: attachment whose scatterlist is to be returned
  247. * @dir: direction of DMA transfer
  248. *
  249. * Calls &drm_driver.gem_prime_get_sg_table and then maps the scatterlist. This
  250. * can be used as the &dma_buf_ops.map_dma_buf callback.
  251. *
  252. * Returns sg_table containing the scatterlist to be returned; returns ERR_PTR
  253. * on error. May return -EINTR if it is interrupted by a signal.
  254. */
  255. struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
  256. enum dma_data_direction dir)
  257. {
  258. struct drm_prime_attachment *prime_attach = attach->priv;
  259. struct drm_gem_object *obj = attach->dmabuf->priv;
  260. struct sg_table *sgt;
  261. if (WARN_ON(dir == DMA_NONE || !prime_attach))
  262. return ERR_PTR(-EINVAL);
  263. /* return the cached mapping when possible */
  264. if (prime_attach->dir == dir)
  265. return prime_attach->sgt;
  266. /*
  267. * two mappings with different directions for the same attachment are
  268. * not allowed
  269. */
  270. if (WARN_ON(prime_attach->dir != DMA_NONE))
  271. return ERR_PTR(-EBUSY);
  272. sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
  273. if (!IS_ERR(sgt)) {
  274. if (!dma_map_sg_attrs(attach->dev, sgt->sgl, sgt->nents, dir,
  275. DMA_ATTR_SKIP_CPU_SYNC)) {
  276. sg_free_table(sgt);
  277. kfree(sgt);
  278. sgt = ERR_PTR(-ENOMEM);
  279. } else {
  280. prime_attach->sgt = sgt;
  281. prime_attach->dir = dir;
  282. }
  283. }
  284. return sgt;
  285. }
  286. EXPORT_SYMBOL(drm_gem_map_dma_buf);
  287. /**
  288. * drm_gem_unmap_dma_buf - unmap_dma_buf implementation for GEM
  289. * @attach: attachment to unmap buffer from
  290. * @sgt: scatterlist info of the buffer to unmap
  291. * @dir: direction of DMA transfer
  292. *
  293. * Not implemented. The unmap is done at drm_gem_map_detach(). This can be
  294. * used as the &dma_buf_ops.unmap_dma_buf callback.
  295. */
  296. void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
  297. struct sg_table *sgt,
  298. enum dma_data_direction dir)
  299. {
  300. /* nothing to be done here */
  301. }
  302. EXPORT_SYMBOL(drm_gem_unmap_dma_buf);
  303. /**
  304. * drm_gem_dmabuf_export - dma_buf export implementation for GEM
  305. * @dev: parent device for the exported dmabuf
  306. * @exp_info: the export information used by dma_buf_export()
  307. *
  308. * This wraps dma_buf_export() for use by generic GEM drivers that are using
  309. * drm_gem_dmabuf_release(). In addition to calling dma_buf_export(), we take
  310. * a reference to the &drm_device and the exported &drm_gem_object (stored in
  311. * &dma_buf_export_info.priv) which is released by drm_gem_dmabuf_release().
  312. *
  313. * Returns the new dmabuf.
  314. */
  315. struct dma_buf *drm_gem_dmabuf_export(struct drm_device *dev,
  316. struct dma_buf_export_info *exp_info)
  317. {
  318. struct dma_buf *dma_buf;
  319. dma_buf = dma_buf_export(exp_info);
  320. if (IS_ERR(dma_buf))
  321. return dma_buf;
  322. drm_dev_get(dev);
  323. drm_gem_object_get(exp_info->priv);
  324. return dma_buf;
  325. }
  326. EXPORT_SYMBOL(drm_gem_dmabuf_export);
  327. /**
  328. * drm_gem_dmabuf_release - dma_buf release implementation for GEM
  329. * @dma_buf: buffer to be released
  330. *
  331. * Generic release function for dma_bufs exported as PRIME buffers. GEM drivers
  332. * must use this in their dma_buf ops structure as the release callback.
  333. * drm_gem_dmabuf_release() should be used in conjunction with
  334. * drm_gem_dmabuf_export().
  335. */
  336. void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
  337. {
  338. struct drm_gem_object *obj = dma_buf->priv;
  339. struct drm_device *dev = obj->dev;
  340. /* drop the reference on the export fd holds */
  341. drm_gem_object_put_unlocked(obj);
  342. drm_dev_put(dev);
  343. }
  344. EXPORT_SYMBOL(drm_gem_dmabuf_release);
  345. /**
  346. * drm_gem_dmabuf_vmap - dma_buf vmap implementation for GEM
  347. * @dma_buf: buffer to be mapped
  348. *
  349. * Sets up a kernel virtual mapping. This can be used as the &dma_buf_ops.vmap
  350. * callback.
  351. *
  352. * Returns the kernel virtual address.
  353. */
  354. void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
  355. {
  356. struct drm_gem_object *obj = dma_buf->priv;
  357. struct drm_device *dev = obj->dev;
  358. if (dev->driver->gem_prime_vmap)
  359. return dev->driver->gem_prime_vmap(obj);
  360. else
  361. return NULL;
  362. }
  363. EXPORT_SYMBOL(drm_gem_dmabuf_vmap);
  364. /**
  365. * drm_gem_dmabuf_vunmap - dma_buf vunmap implementation for GEM
  366. * @dma_buf: buffer to be unmapped
  367. * @vaddr: the virtual address of the buffer
  368. *
  369. * Releases a kernel virtual mapping. This can be used as the
  370. * &dma_buf_ops.vunmap callback.
  371. */
  372. void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
  373. {
  374. struct drm_gem_object *obj = dma_buf->priv;
  375. struct drm_device *dev = obj->dev;
  376. if (dev->driver->gem_prime_vunmap)
  377. dev->driver->gem_prime_vunmap(obj, vaddr);
  378. }
  379. EXPORT_SYMBOL(drm_gem_dmabuf_vunmap);
  380. /**
  381. * drm_gem_dmabuf_kmap_atomic - map_atomic implementation for GEM
  382. * @dma_buf: buffer to be mapped
  383. * @page_num: page number within the buffer
  384. *
  385. * Not implemented. This can be used as the &dma_buf_ops.map_atomic callback.
  386. */
  387. void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
  388. unsigned long page_num)
  389. {
  390. return NULL;
  391. }
  392. EXPORT_SYMBOL(drm_gem_dmabuf_kmap_atomic);
  393. /**
  394. * drm_gem_dmabuf_kunmap_atomic - unmap_atomic implementation for GEM
  395. * @dma_buf: buffer to be unmapped
  396. * @page_num: page number within the buffer
  397. * @addr: virtual address of the buffer
  398. *
  399. * Not implemented. This can be used as the &dma_buf_ops.unmap_atomic callback.
  400. */
  401. void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
  402. unsigned long page_num, void *addr)
  403. {
  404. }
  405. EXPORT_SYMBOL(drm_gem_dmabuf_kunmap_atomic);
  406. /**
  407. * drm_gem_dmabuf_kmap - map implementation for GEM
  408. * @dma_buf: buffer to be mapped
  409. * @page_num: page number within the buffer
  410. *
  411. * Not implemented. This can be used as the &dma_buf_ops.map callback.
  412. */
  413. void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
  414. {
  415. return NULL;
  416. }
  417. EXPORT_SYMBOL(drm_gem_dmabuf_kmap);
  418. /**
  419. * drm_gem_dmabuf_kunmap - unmap implementation for GEM
  420. * @dma_buf: buffer to be unmapped
  421. * @page_num: page number within the buffer
  422. * @addr: virtual address of the buffer
  423. *
  424. * Not implemented. This can be used as the &dma_buf_ops.unmap callback.
  425. */
  426. void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num,
  427. void *addr)
  428. {
  429. }
  430. EXPORT_SYMBOL(drm_gem_dmabuf_kunmap);
  431. /**
  432. * drm_gem_dmabuf_mmap - dma_buf mmap implementation for GEM
  433. * @dma_buf: buffer to be mapped
  434. * @vma: virtual address range
  435. *
  436. * Provides memory mapping for the buffer. This can be used as the
  437. * &dma_buf_ops.mmap callback.
  438. *
  439. * Returns 0 on success or a negative error code on failure.
  440. */
  441. int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
  442. {
  443. struct drm_gem_object *obj = dma_buf->priv;
  444. struct drm_device *dev = obj->dev;
  445. if (!dev->driver->gem_prime_mmap)
  446. return -ENOSYS;
  447. return dev->driver->gem_prime_mmap(obj, vma);
  448. }
  449. EXPORT_SYMBOL(drm_gem_dmabuf_mmap);
  450. static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
  451. .attach = drm_gem_map_attach,
  452. .detach = drm_gem_map_detach,
  453. .map_dma_buf = drm_gem_map_dma_buf,
  454. .unmap_dma_buf = drm_gem_unmap_dma_buf,
  455. .release = drm_gem_dmabuf_release,
  456. .map = drm_gem_dmabuf_kmap,
  457. .map_atomic = drm_gem_dmabuf_kmap_atomic,
  458. .unmap = drm_gem_dmabuf_kunmap,
  459. .unmap_atomic = drm_gem_dmabuf_kunmap_atomic,
  460. .mmap = drm_gem_dmabuf_mmap,
  461. .vmap = drm_gem_dmabuf_vmap,
  462. .vunmap = drm_gem_dmabuf_vunmap,
  463. };
  464. /**
  465. * DOC: PRIME Helpers
  466. *
  467. * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
  468. * simpler APIs by using the helper functions @drm_gem_prime_export and
  469. * @drm_gem_prime_import. These functions implement dma-buf support in terms of
  470. * six lower-level driver callbacks:
  471. *
  472. * Export callbacks:
  473. *
  474. * * @gem_prime_pin (optional): prepare a GEM object for exporting
  475. * * @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
  476. * * @gem_prime_vmap: vmap a buffer exported by your driver
  477. * * @gem_prime_vunmap: vunmap a buffer exported by your driver
  478. * * @gem_prime_mmap (optional): mmap a buffer exported by your driver
  479. *
  480. * Import callback:
  481. *
  482. * * @gem_prime_import_sg_table (import): produce a GEM object from another
  483. * driver's scatter/gather table
  484. */
  485. /**
  486. * drm_gem_prime_export - helper library implementation of the export callback
  487. * @dev: drm_device to export from
  488. * @obj: GEM object to export
  489. * @flags: flags like DRM_CLOEXEC and DRM_RDWR
  490. *
  491. * This is the implementation of the gem_prime_export functions for GEM drivers
  492. * using the PRIME helpers.
  493. */
  494. struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
  495. struct drm_gem_object *obj,
  496. int flags)
  497. {
  498. struct dma_buf_export_info exp_info = {
  499. .exp_name = KBUILD_MODNAME, /* white lie for debug */
  500. .owner = dev->driver->fops->owner,
  501. .ops = &drm_gem_prime_dmabuf_ops,
  502. .size = obj->size,
  503. .flags = flags,
  504. .priv = obj,
  505. };
  506. if (dev->driver->gem_prime_res_obj)
  507. exp_info.resv = dev->driver->gem_prime_res_obj(obj);
  508. return drm_gem_dmabuf_export(dev, &exp_info);
  509. }
  510. EXPORT_SYMBOL(drm_gem_prime_export);
  511. static struct dma_buf *export_and_register_object(struct drm_device *dev,
  512. struct drm_gem_object *obj,
  513. uint32_t flags)
  514. {
  515. struct dma_buf *dmabuf;
  516. /* prevent races with concurrent gem_close. */
  517. if (obj->handle_count == 0) {
  518. dmabuf = ERR_PTR(-ENOENT);
  519. return dmabuf;
  520. }
  521. dmabuf = dev->driver->gem_prime_export(dev, obj, flags);
  522. if (IS_ERR(dmabuf)) {
  523. /* normally the created dma-buf takes ownership of the ref,
  524. * but if that fails then drop the ref
  525. */
  526. return dmabuf;
  527. }
  528. /*
  529. * Note that callers do not need to clean up the export cache
  530. * since the check for obj->handle_count guarantees that someone
  531. * will clean it up.
  532. */
  533. obj->dma_buf = dmabuf;
  534. get_dma_buf(obj->dma_buf);
  535. return dmabuf;
  536. }
  537. /**
  538. * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers
  539. * @dev: dev to export the buffer from
  540. * @file_priv: drm file-private structure
  541. * @handle: buffer handle to export
  542. * @flags: flags like DRM_CLOEXEC
  543. * @prime_fd: pointer to storage for the fd id of the create dma-buf
  544. *
  545. * This is the PRIME export function which must be used mandatorily by GEM
  546. * drivers to ensure correct lifetime management of the underlying GEM object.
  547. * The actual exporting from GEM object to a dma-buf is done through the
  548. * gem_prime_export driver callback.
  549. */
  550. int drm_gem_prime_handle_to_fd(struct drm_device *dev,
  551. struct drm_file *file_priv, uint32_t handle,
  552. uint32_t flags,
  553. int *prime_fd)
  554. {
  555. struct drm_gem_object *obj;
  556. int ret = 0;
  557. struct dma_buf *dmabuf;
  558. mutex_lock(&file_priv->prime.lock);
  559. obj = drm_gem_object_lookup(file_priv, handle);
  560. if (!obj) {
  561. ret = -ENOENT;
  562. goto out_unlock;
  563. }
  564. dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle);
  565. if (dmabuf) {
  566. get_dma_buf(dmabuf);
  567. goto out_have_handle;
  568. }
  569. mutex_lock(&dev->object_name_lock);
  570. /* re-export the original imported object */
  571. if (obj->import_attach) {
  572. dmabuf = obj->import_attach->dmabuf;
  573. get_dma_buf(dmabuf);
  574. goto out_have_obj;
  575. }
  576. if (obj->dma_buf) {
  577. get_dma_buf(obj->dma_buf);
  578. dmabuf = obj->dma_buf;
  579. goto out_have_obj;
  580. }
  581. dmabuf = export_and_register_object(dev, obj, flags);
  582. if (IS_ERR(dmabuf)) {
  583. /* normally the created dma-buf takes ownership of the ref,
  584. * but if that fails then drop the ref
  585. */
  586. ret = PTR_ERR(dmabuf);
  587. mutex_unlock(&dev->object_name_lock);
  588. goto out;
  589. }
  590. out_have_obj:
  591. /*
  592. * If we've exported this buffer then cheat and add it to the import list
  593. * so we get the correct handle back. We must do this under the
  594. * protection of dev->object_name_lock to ensure that a racing gem close
  595. * ioctl doesn't miss to remove this buffer handle from the cache.
  596. */
  597. ret = drm_prime_add_buf_handle(&file_priv->prime,
  598. dmabuf, handle);
  599. mutex_unlock(&dev->object_name_lock);
  600. if (ret)
  601. goto fail_put_dmabuf;
  602. out_have_handle:
  603. ret = dma_buf_fd(dmabuf, flags);
  604. /*
  605. * We must _not_ remove the buffer from the handle cache since the newly
  606. * created dma buf is already linked in the global obj->dma_buf pointer,
  607. * and that is invariant as long as a userspace gem handle exists.
  608. * Closing the handle will clean out the cache anyway, so we don't leak.
  609. */
  610. if (ret < 0) {
  611. goto fail_put_dmabuf;
  612. } else {
  613. *prime_fd = ret;
  614. ret = 0;
  615. }
  616. goto out;
  617. fail_put_dmabuf:
  618. dma_buf_put(dmabuf);
  619. out:
  620. drm_gem_object_put_unlocked(obj);
  621. out_unlock:
  622. mutex_unlock(&file_priv->prime.lock);
  623. return ret;
  624. }
  625. EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
  626. /**
  627. * drm_gem_prime_import_dev - core implementation of the import callback
  628. * @dev: drm_device to import into
  629. * @dma_buf: dma-buf object to import
  630. * @attach_dev: struct device to dma_buf attach
  631. *
  632. * This is the core of drm_gem_prime_import. It's designed to be called by
  633. * drivers who want to use a different device structure than dev->dev for
  634. * attaching via dma_buf.
  635. */
  636. struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev,
  637. struct dma_buf *dma_buf,
  638. struct device *attach_dev)
  639. {
  640. struct dma_buf_attachment *attach;
  641. struct sg_table *sgt;
  642. struct drm_gem_object *obj;
  643. int ret;
  644. if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
  645. obj = dma_buf->priv;
  646. if (obj->dev == dev) {
  647. /*
  648. * Importing dmabuf exported from out own gem increases
  649. * refcount on gem itself instead of f_count of dmabuf.
  650. */
  651. drm_gem_object_get(obj);
  652. return obj;
  653. }
  654. }
  655. if (!dev->driver->gem_prime_import_sg_table)
  656. return ERR_PTR(-EINVAL);
  657. attach = dma_buf_attach(dma_buf, attach_dev);
  658. if (IS_ERR(attach))
  659. return ERR_CAST(attach);
  660. get_dma_buf(dma_buf);
  661. sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  662. if (IS_ERR(sgt)) {
  663. ret = PTR_ERR(sgt);
  664. goto fail_detach;
  665. }
  666. obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
  667. if (IS_ERR(obj)) {
  668. ret = PTR_ERR(obj);
  669. goto fail_unmap;
  670. }
  671. obj->import_attach = attach;
  672. return obj;
  673. fail_unmap:
  674. dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
  675. fail_detach:
  676. dma_buf_detach(dma_buf, attach);
  677. dma_buf_put(dma_buf);
  678. return ERR_PTR(ret);
  679. }
  680. EXPORT_SYMBOL(drm_gem_prime_import_dev);
  681. /**
  682. * drm_gem_prime_import - helper library implementation of the import callback
  683. * @dev: drm_device to import into
  684. * @dma_buf: dma-buf object to import
  685. *
  686. * This is the implementation of the gem_prime_import functions for GEM drivers
  687. * using the PRIME helpers.
  688. */
  689. struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
  690. struct dma_buf *dma_buf)
  691. {
  692. return drm_gem_prime_import_dev(dev, dma_buf, dev->dev);
  693. }
  694. EXPORT_SYMBOL(drm_gem_prime_import);
  695. /**
  696. * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers
  697. * @dev: dev to export the buffer from
  698. * @file_priv: drm file-private structure
  699. * @prime_fd: fd id of the dma-buf which should be imported
  700. * @handle: pointer to storage for the handle of the imported buffer object
  701. *
  702. * This is the PRIME import function which must be used mandatorily by GEM
  703. * drivers to ensure correct lifetime management of the underlying GEM object.
  704. * The actual importing of GEM object from the dma-buf is done through the
  705. * gem_import_export driver callback.
  706. */
  707. int drm_gem_prime_fd_to_handle(struct drm_device *dev,
  708. struct drm_file *file_priv, int prime_fd,
  709. uint32_t *handle)
  710. {
  711. struct dma_buf *dma_buf;
  712. struct drm_gem_object *obj;
  713. int ret;
  714. dma_buf = dma_buf_get(prime_fd);
  715. if (IS_ERR(dma_buf))
  716. return PTR_ERR(dma_buf);
  717. mutex_lock(&file_priv->prime.lock);
  718. ret = drm_prime_lookup_buf_handle(&file_priv->prime,
  719. dma_buf, handle);
  720. if (ret == 0)
  721. goto out_put;
  722. /* never seen this one, need to import */
  723. mutex_lock(&dev->object_name_lock);
  724. obj = dev->driver->gem_prime_import(dev, dma_buf);
  725. if (IS_ERR(obj)) {
  726. ret = PTR_ERR(obj);
  727. goto out_unlock;
  728. }
  729. if (obj->dma_buf) {
  730. WARN_ON(obj->dma_buf != dma_buf);
  731. } else {
  732. obj->dma_buf = dma_buf;
  733. get_dma_buf(dma_buf);
  734. }
  735. /* _handle_create_tail unconditionally unlocks dev->object_name_lock. */
  736. ret = drm_gem_handle_create_tail(file_priv, obj, handle);
  737. drm_gem_object_put_unlocked(obj);
  738. if (ret)
  739. goto out_put;
  740. ret = drm_prime_add_buf_handle(&file_priv->prime,
  741. dma_buf, *handle);
  742. mutex_unlock(&file_priv->prime.lock);
  743. if (ret)
  744. goto fail;
  745. dma_buf_put(dma_buf);
  746. return 0;
  747. fail:
  748. /* hmm, if driver attached, we are relying on the free-object path
  749. * to detach.. which seems ok..
  750. */
  751. drm_gem_handle_delete(file_priv, *handle);
  752. dma_buf_put(dma_buf);
  753. return ret;
  754. out_unlock:
  755. mutex_unlock(&dev->object_name_lock);
  756. out_put:
  757. mutex_unlock(&file_priv->prime.lock);
  758. dma_buf_put(dma_buf);
  759. return ret;
  760. }
  761. EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
  762. int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
  763. struct drm_file *file_priv)
  764. {
  765. struct drm_prime_handle *args = data;
  766. if (!drm_core_check_feature(dev, DRIVER_PRIME))
  767. return -EINVAL;
  768. if (!dev->driver->prime_handle_to_fd)
  769. return -ENOSYS;
  770. /* check flags are valid */
  771. if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR))
  772. return -EINVAL;
  773. return dev->driver->prime_handle_to_fd(dev, file_priv,
  774. args->handle, args->flags, &args->fd);
  775. }
  776. int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
  777. struct drm_file *file_priv)
  778. {
  779. struct drm_prime_handle *args = data;
  780. if (!drm_core_check_feature(dev, DRIVER_PRIME))
  781. return -EINVAL;
  782. if (!dev->driver->prime_fd_to_handle)
  783. return -ENOSYS;
  784. return dev->driver->prime_fd_to_handle(dev, file_priv,
  785. args->fd, &args->handle);
  786. }
  787. /**
  788. * drm_prime_pages_to_sg - converts a page array into an sg list
  789. * @pages: pointer to the array of page pointers to convert
  790. * @nr_pages: length of the page vector
  791. *
  792. * This helper creates an sg table object from a set of pages
  793. * the driver is responsible for mapping the pages into the
  794. * importers address space for use with dma_buf itself.
  795. */
  796. struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages)
  797. {
  798. struct sg_table *sg = NULL;
  799. int ret;
  800. sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
  801. if (!sg) {
  802. ret = -ENOMEM;
  803. goto out;
  804. }
  805. ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
  806. nr_pages << PAGE_SHIFT, GFP_KERNEL);
  807. if (ret)
  808. goto out;
  809. return sg;
  810. out:
  811. kfree(sg);
  812. return ERR_PTR(ret);
  813. }
  814. EXPORT_SYMBOL(drm_prime_pages_to_sg);
  815. /**
  816. * drm_prime_sg_to_page_addr_arrays - convert an sg table into a page array
  817. * @sgt: scatter-gather table to convert
  818. * @pages: optional array of page pointers to store the page array in
  819. * @addrs: optional array to store the dma bus address of each page
  820. * @max_entries: size of both the passed-in arrays
  821. *
  822. * Exports an sg table into an array of pages and addresses. This is currently
  823. * required by the TTM driver in order to do correct fault handling.
  824. */
  825. int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
  826. dma_addr_t *addrs, int max_entries)
  827. {
  828. unsigned count;
  829. struct scatterlist *sg;
  830. struct page *page;
  831. u32 len, index;
  832. dma_addr_t addr;
  833. index = 0;
  834. for_each_sg(sgt->sgl, sg, sgt->nents, count) {
  835. len = sg->length;
  836. page = sg_page(sg);
  837. addr = sg_dma_address(sg);
  838. while (len > 0) {
  839. if (WARN_ON(index >= max_entries))
  840. return -1;
  841. if (pages)
  842. pages[index] = page;
  843. if (addrs)
  844. addrs[index] = addr;
  845. page++;
  846. addr += PAGE_SIZE;
  847. len -= PAGE_SIZE;
  848. index++;
  849. }
  850. }
  851. return 0;
  852. }
  853. EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
  854. /**
  855. * drm_prime_gem_destroy - helper to clean up a PRIME-imported GEM object
  856. * @obj: GEM object which was created from a dma-buf
  857. * @sg: the sg-table which was pinned at import time
  858. *
  859. * This is the cleanup functions which GEM drivers need to call when they use
  860. * @drm_gem_prime_import to import dma-bufs.
  861. */
  862. void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
  863. {
  864. struct dma_buf_attachment *attach;
  865. struct dma_buf *dma_buf;
  866. attach = obj->import_attach;
  867. if (sg)
  868. dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
  869. dma_buf = attach->dmabuf;
  870. dma_buf_detach(attach->dmabuf, attach);
  871. /* remove the reference */
  872. dma_buf_put(dma_buf);
  873. }
  874. EXPORT_SYMBOL(drm_prime_gem_destroy);
  875. void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
  876. {
  877. mutex_init(&prime_fpriv->lock);
  878. prime_fpriv->dmabufs = RB_ROOT;
  879. prime_fpriv->handles = RB_ROOT;
  880. }
  881. void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
  882. {
  883. /* by now drm_gem_release should've made sure the list is empty */
  884. WARN_ON(!RB_EMPTY_ROOT(&prime_fpriv->dmabufs));
  885. }