drm_prime.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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. return dev->driver->gem_prime_vmap(obj);
  359. }
  360. EXPORT_SYMBOL(drm_gem_dmabuf_vmap);
  361. /**
  362. * drm_gem_dmabuf_vunmap - dma_buf vunmap implementation for GEM
  363. * @dma_buf: buffer to be unmapped
  364. * @vaddr: the virtual address of the buffer
  365. *
  366. * Releases a kernel virtual mapping. This can be used as the
  367. * &dma_buf_ops.vunmap callback.
  368. */
  369. void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
  370. {
  371. struct drm_gem_object *obj = dma_buf->priv;
  372. struct drm_device *dev = obj->dev;
  373. dev->driver->gem_prime_vunmap(obj, vaddr);
  374. }
  375. EXPORT_SYMBOL(drm_gem_dmabuf_vunmap);
  376. /**
  377. * drm_gem_dmabuf_kmap_atomic - map_atomic implementation for GEM
  378. * @dma_buf: buffer to be mapped
  379. * @page_num: page number within the buffer
  380. *
  381. * Not implemented. This can be used as the &dma_buf_ops.map_atomic callback.
  382. */
  383. void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
  384. unsigned long page_num)
  385. {
  386. return NULL;
  387. }
  388. EXPORT_SYMBOL(drm_gem_dmabuf_kmap_atomic);
  389. /**
  390. * drm_gem_dmabuf_kunmap_atomic - unmap_atomic implementation for GEM
  391. * @dma_buf: buffer to be unmapped
  392. * @page_num: page number within the buffer
  393. * @addr: virtual address of the buffer
  394. *
  395. * Not implemented. This can be used as the &dma_buf_ops.unmap_atomic callback.
  396. */
  397. void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
  398. unsigned long page_num, void *addr)
  399. {
  400. }
  401. EXPORT_SYMBOL(drm_gem_dmabuf_kunmap_atomic);
  402. /**
  403. * drm_gem_dmabuf_kmap - map implementation for GEM
  404. * @dma_buf: buffer to be mapped
  405. * @page_num: page number within the buffer
  406. *
  407. * Not implemented. This can be used as the &dma_buf_ops.map callback.
  408. */
  409. void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
  410. {
  411. return NULL;
  412. }
  413. EXPORT_SYMBOL(drm_gem_dmabuf_kmap);
  414. /**
  415. * drm_gem_dmabuf_kunmap - unmap implementation for GEM
  416. * @dma_buf: buffer to be unmapped
  417. * @page_num: page number within the buffer
  418. * @addr: virtual address of the buffer
  419. *
  420. * Not implemented. This can be used as the &dma_buf_ops.unmap callback.
  421. */
  422. void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num,
  423. void *addr)
  424. {
  425. }
  426. EXPORT_SYMBOL(drm_gem_dmabuf_kunmap);
  427. /**
  428. * drm_gem_dmabuf_mmap - dma_buf mmap implementation for GEM
  429. * @dma_buf: buffer to be mapped
  430. * @vma: virtual address range
  431. *
  432. * Provides memory mapping for the buffer. This can be used as the
  433. * &dma_buf_ops.mmap callback.
  434. *
  435. * Returns 0 on success or a negative error code on failure.
  436. */
  437. int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
  438. {
  439. struct drm_gem_object *obj = dma_buf->priv;
  440. struct drm_device *dev = obj->dev;
  441. if (!dev->driver->gem_prime_mmap)
  442. return -ENOSYS;
  443. return dev->driver->gem_prime_mmap(obj, vma);
  444. }
  445. EXPORT_SYMBOL(drm_gem_dmabuf_mmap);
  446. static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
  447. .attach = drm_gem_map_attach,
  448. .detach = drm_gem_map_detach,
  449. .map_dma_buf = drm_gem_map_dma_buf,
  450. .unmap_dma_buf = drm_gem_unmap_dma_buf,
  451. .release = drm_gem_dmabuf_release,
  452. .map = drm_gem_dmabuf_kmap,
  453. .map_atomic = drm_gem_dmabuf_kmap_atomic,
  454. .unmap = drm_gem_dmabuf_kunmap,
  455. .unmap_atomic = drm_gem_dmabuf_kunmap_atomic,
  456. .mmap = drm_gem_dmabuf_mmap,
  457. .vmap = drm_gem_dmabuf_vmap,
  458. .vunmap = drm_gem_dmabuf_vunmap,
  459. };
  460. /**
  461. * DOC: PRIME Helpers
  462. *
  463. * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
  464. * simpler APIs by using the helper functions @drm_gem_prime_export and
  465. * @drm_gem_prime_import. These functions implement dma-buf support in terms of
  466. * six lower-level driver callbacks:
  467. *
  468. * Export callbacks:
  469. *
  470. * * @gem_prime_pin (optional): prepare a GEM object for exporting
  471. * * @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
  472. * * @gem_prime_vmap: vmap a buffer exported by your driver
  473. * * @gem_prime_vunmap: vunmap a buffer exported by your driver
  474. * * @gem_prime_mmap (optional): mmap a buffer exported by your driver
  475. *
  476. * Import callback:
  477. *
  478. * * @gem_prime_import_sg_table (import): produce a GEM object from another
  479. * driver's scatter/gather table
  480. */
  481. /**
  482. * drm_gem_prime_export - helper library implementation of the export callback
  483. * @dev: drm_device to export from
  484. * @obj: GEM object to export
  485. * @flags: flags like DRM_CLOEXEC and DRM_RDWR
  486. *
  487. * This is the implementation of the gem_prime_export functions for GEM drivers
  488. * using the PRIME helpers.
  489. */
  490. struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
  491. struct drm_gem_object *obj,
  492. int flags)
  493. {
  494. struct dma_buf_export_info exp_info = {
  495. .exp_name = KBUILD_MODNAME, /* white lie for debug */
  496. .owner = dev->driver->fops->owner,
  497. .ops = &drm_gem_prime_dmabuf_ops,
  498. .size = obj->size,
  499. .flags = flags,
  500. .priv = obj,
  501. };
  502. if (dev->driver->gem_prime_res_obj)
  503. exp_info.resv = dev->driver->gem_prime_res_obj(obj);
  504. return drm_gem_dmabuf_export(dev, &exp_info);
  505. }
  506. EXPORT_SYMBOL(drm_gem_prime_export);
  507. static struct dma_buf *export_and_register_object(struct drm_device *dev,
  508. struct drm_gem_object *obj,
  509. uint32_t flags)
  510. {
  511. struct dma_buf *dmabuf;
  512. /* prevent races with concurrent gem_close. */
  513. if (obj->handle_count == 0) {
  514. dmabuf = ERR_PTR(-ENOENT);
  515. return dmabuf;
  516. }
  517. dmabuf = dev->driver->gem_prime_export(dev, obj, flags);
  518. if (IS_ERR(dmabuf)) {
  519. /* normally the created dma-buf takes ownership of the ref,
  520. * but if that fails then drop the ref
  521. */
  522. return dmabuf;
  523. }
  524. /*
  525. * Note that callers do not need to clean up the export cache
  526. * since the check for obj->handle_count guarantees that someone
  527. * will clean it up.
  528. */
  529. obj->dma_buf = dmabuf;
  530. get_dma_buf(obj->dma_buf);
  531. return dmabuf;
  532. }
  533. /**
  534. * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers
  535. * @dev: dev to export the buffer from
  536. * @file_priv: drm file-private structure
  537. * @handle: buffer handle to export
  538. * @flags: flags like DRM_CLOEXEC
  539. * @prime_fd: pointer to storage for the fd id of the create dma-buf
  540. *
  541. * This is the PRIME export function which must be used mandatorily by GEM
  542. * drivers to ensure correct lifetime management of the underlying GEM object.
  543. * The actual exporting from GEM object to a dma-buf is done through the
  544. * gem_prime_export driver callback.
  545. */
  546. int drm_gem_prime_handle_to_fd(struct drm_device *dev,
  547. struct drm_file *file_priv, uint32_t handle,
  548. uint32_t flags,
  549. int *prime_fd)
  550. {
  551. struct drm_gem_object *obj;
  552. int ret = 0;
  553. struct dma_buf *dmabuf;
  554. mutex_lock(&file_priv->prime.lock);
  555. obj = drm_gem_object_lookup(file_priv, handle);
  556. if (!obj) {
  557. ret = -ENOENT;
  558. goto out_unlock;
  559. }
  560. dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle);
  561. if (dmabuf) {
  562. get_dma_buf(dmabuf);
  563. goto out_have_handle;
  564. }
  565. mutex_lock(&dev->object_name_lock);
  566. /* re-export the original imported object */
  567. if (obj->import_attach) {
  568. dmabuf = obj->import_attach->dmabuf;
  569. get_dma_buf(dmabuf);
  570. goto out_have_obj;
  571. }
  572. if (obj->dma_buf) {
  573. get_dma_buf(obj->dma_buf);
  574. dmabuf = obj->dma_buf;
  575. goto out_have_obj;
  576. }
  577. dmabuf = export_and_register_object(dev, obj, flags);
  578. if (IS_ERR(dmabuf)) {
  579. /* normally the created dma-buf takes ownership of the ref,
  580. * but if that fails then drop the ref
  581. */
  582. ret = PTR_ERR(dmabuf);
  583. mutex_unlock(&dev->object_name_lock);
  584. goto out;
  585. }
  586. out_have_obj:
  587. /*
  588. * If we've exported this buffer then cheat and add it to the import list
  589. * so we get the correct handle back. We must do this under the
  590. * protection of dev->object_name_lock to ensure that a racing gem close
  591. * ioctl doesn't miss to remove this buffer handle from the cache.
  592. */
  593. ret = drm_prime_add_buf_handle(&file_priv->prime,
  594. dmabuf, handle);
  595. mutex_unlock(&dev->object_name_lock);
  596. if (ret)
  597. goto fail_put_dmabuf;
  598. out_have_handle:
  599. ret = dma_buf_fd(dmabuf, flags);
  600. /*
  601. * We must _not_ remove the buffer from the handle cache since the newly
  602. * created dma buf is already linked in the global obj->dma_buf pointer,
  603. * and that is invariant as long as a userspace gem handle exists.
  604. * Closing the handle will clean out the cache anyway, so we don't leak.
  605. */
  606. if (ret < 0) {
  607. goto fail_put_dmabuf;
  608. } else {
  609. *prime_fd = ret;
  610. ret = 0;
  611. }
  612. goto out;
  613. fail_put_dmabuf:
  614. dma_buf_put(dmabuf);
  615. out:
  616. drm_gem_object_put_unlocked(obj);
  617. out_unlock:
  618. mutex_unlock(&file_priv->prime.lock);
  619. return ret;
  620. }
  621. EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
  622. /**
  623. * drm_gem_prime_import_dev - core implementation of the import callback
  624. * @dev: drm_device to import into
  625. * @dma_buf: dma-buf object to import
  626. * @attach_dev: struct device to dma_buf attach
  627. *
  628. * This is the core of drm_gem_prime_import. It's designed to be called by
  629. * drivers who want to use a different device structure than dev->dev for
  630. * attaching via dma_buf.
  631. */
  632. struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev,
  633. struct dma_buf *dma_buf,
  634. struct device *attach_dev)
  635. {
  636. struct dma_buf_attachment *attach;
  637. struct sg_table *sgt;
  638. struct drm_gem_object *obj;
  639. int ret;
  640. if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
  641. obj = dma_buf->priv;
  642. if (obj->dev == dev) {
  643. /*
  644. * Importing dmabuf exported from out own gem increases
  645. * refcount on gem itself instead of f_count of dmabuf.
  646. */
  647. drm_gem_object_get(obj);
  648. return obj;
  649. }
  650. }
  651. if (!dev->driver->gem_prime_import_sg_table)
  652. return ERR_PTR(-EINVAL);
  653. attach = dma_buf_attach(dma_buf, attach_dev);
  654. if (IS_ERR(attach))
  655. return ERR_CAST(attach);
  656. get_dma_buf(dma_buf);
  657. sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  658. if (IS_ERR(sgt)) {
  659. ret = PTR_ERR(sgt);
  660. goto fail_detach;
  661. }
  662. obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
  663. if (IS_ERR(obj)) {
  664. ret = PTR_ERR(obj);
  665. goto fail_unmap;
  666. }
  667. obj->import_attach = attach;
  668. return obj;
  669. fail_unmap:
  670. dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
  671. fail_detach:
  672. dma_buf_detach(dma_buf, attach);
  673. dma_buf_put(dma_buf);
  674. return ERR_PTR(ret);
  675. }
  676. EXPORT_SYMBOL(drm_gem_prime_import_dev);
  677. /**
  678. * drm_gem_prime_import - helper library implementation of the import callback
  679. * @dev: drm_device to import into
  680. * @dma_buf: dma-buf object to import
  681. *
  682. * This is the implementation of the gem_prime_import functions for GEM drivers
  683. * using the PRIME helpers.
  684. */
  685. struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
  686. struct dma_buf *dma_buf)
  687. {
  688. return drm_gem_prime_import_dev(dev, dma_buf, dev->dev);
  689. }
  690. EXPORT_SYMBOL(drm_gem_prime_import);
  691. /**
  692. * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers
  693. * @dev: dev to export the buffer from
  694. * @file_priv: drm file-private structure
  695. * @prime_fd: fd id of the dma-buf which should be imported
  696. * @handle: pointer to storage for the handle of the imported buffer object
  697. *
  698. * This is the PRIME import function which must be used mandatorily by GEM
  699. * drivers to ensure correct lifetime management of the underlying GEM object.
  700. * The actual importing of GEM object from the dma-buf is done through the
  701. * gem_import_export driver callback.
  702. */
  703. int drm_gem_prime_fd_to_handle(struct drm_device *dev,
  704. struct drm_file *file_priv, int prime_fd,
  705. uint32_t *handle)
  706. {
  707. struct dma_buf *dma_buf;
  708. struct drm_gem_object *obj;
  709. int ret;
  710. dma_buf = dma_buf_get(prime_fd);
  711. if (IS_ERR(dma_buf))
  712. return PTR_ERR(dma_buf);
  713. mutex_lock(&file_priv->prime.lock);
  714. ret = drm_prime_lookup_buf_handle(&file_priv->prime,
  715. dma_buf, handle);
  716. if (ret == 0)
  717. goto out_put;
  718. /* never seen this one, need to import */
  719. mutex_lock(&dev->object_name_lock);
  720. obj = dev->driver->gem_prime_import(dev, dma_buf);
  721. if (IS_ERR(obj)) {
  722. ret = PTR_ERR(obj);
  723. goto out_unlock;
  724. }
  725. if (obj->dma_buf) {
  726. WARN_ON(obj->dma_buf != dma_buf);
  727. } else {
  728. obj->dma_buf = dma_buf;
  729. get_dma_buf(dma_buf);
  730. }
  731. /* _handle_create_tail unconditionally unlocks dev->object_name_lock. */
  732. ret = drm_gem_handle_create_tail(file_priv, obj, handle);
  733. drm_gem_object_put_unlocked(obj);
  734. if (ret)
  735. goto out_put;
  736. ret = drm_prime_add_buf_handle(&file_priv->prime,
  737. dma_buf, *handle);
  738. mutex_unlock(&file_priv->prime.lock);
  739. if (ret)
  740. goto fail;
  741. dma_buf_put(dma_buf);
  742. return 0;
  743. fail:
  744. /* hmm, if driver attached, we are relying on the free-object path
  745. * to detach.. which seems ok..
  746. */
  747. drm_gem_handle_delete(file_priv, *handle);
  748. dma_buf_put(dma_buf);
  749. return ret;
  750. out_unlock:
  751. mutex_unlock(&dev->object_name_lock);
  752. out_put:
  753. mutex_unlock(&file_priv->prime.lock);
  754. dma_buf_put(dma_buf);
  755. return ret;
  756. }
  757. EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
  758. int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
  759. struct drm_file *file_priv)
  760. {
  761. struct drm_prime_handle *args = data;
  762. if (!drm_core_check_feature(dev, DRIVER_PRIME))
  763. return -EINVAL;
  764. if (!dev->driver->prime_handle_to_fd)
  765. return -ENOSYS;
  766. /* check flags are valid */
  767. if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR))
  768. return -EINVAL;
  769. return dev->driver->prime_handle_to_fd(dev, file_priv,
  770. args->handle, args->flags, &args->fd);
  771. }
  772. int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
  773. struct drm_file *file_priv)
  774. {
  775. struct drm_prime_handle *args = data;
  776. if (!drm_core_check_feature(dev, DRIVER_PRIME))
  777. return -EINVAL;
  778. if (!dev->driver->prime_fd_to_handle)
  779. return -ENOSYS;
  780. return dev->driver->prime_fd_to_handle(dev, file_priv,
  781. args->fd, &args->handle);
  782. }
  783. /**
  784. * drm_prime_pages_to_sg - converts a page array into an sg list
  785. * @pages: pointer to the array of page pointers to convert
  786. * @nr_pages: length of the page vector
  787. *
  788. * This helper creates an sg table object from a set of pages
  789. * the driver is responsible for mapping the pages into the
  790. * importers address space for use with dma_buf itself.
  791. */
  792. struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages)
  793. {
  794. struct sg_table *sg = NULL;
  795. int ret;
  796. sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
  797. if (!sg) {
  798. ret = -ENOMEM;
  799. goto out;
  800. }
  801. ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
  802. nr_pages << PAGE_SHIFT, GFP_KERNEL);
  803. if (ret)
  804. goto out;
  805. return sg;
  806. out:
  807. kfree(sg);
  808. return ERR_PTR(ret);
  809. }
  810. EXPORT_SYMBOL(drm_prime_pages_to_sg);
  811. /**
  812. * drm_prime_sg_to_page_addr_arrays - convert an sg table into a page array
  813. * @sgt: scatter-gather table to convert
  814. * @pages: optional array of page pointers to store the page array in
  815. * @addrs: optional array to store the dma bus address of each page
  816. * @max_entries: size of both the passed-in arrays
  817. *
  818. * Exports an sg table into an array of pages and addresses. This is currently
  819. * required by the TTM driver in order to do correct fault handling.
  820. */
  821. int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
  822. dma_addr_t *addrs, int max_entries)
  823. {
  824. unsigned count;
  825. struct scatterlist *sg;
  826. struct page *page;
  827. u32 len, index;
  828. dma_addr_t addr;
  829. index = 0;
  830. for_each_sg(sgt->sgl, sg, sgt->nents, count) {
  831. len = sg->length;
  832. page = sg_page(sg);
  833. addr = sg_dma_address(sg);
  834. while (len > 0) {
  835. if (WARN_ON(index >= max_entries))
  836. return -1;
  837. if (pages)
  838. pages[index] = page;
  839. if (addrs)
  840. addrs[index] = addr;
  841. page++;
  842. addr += PAGE_SIZE;
  843. len -= PAGE_SIZE;
  844. index++;
  845. }
  846. }
  847. return 0;
  848. }
  849. EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
  850. /**
  851. * drm_prime_gem_destroy - helper to clean up a PRIME-imported GEM object
  852. * @obj: GEM object which was created from a dma-buf
  853. * @sg: the sg-table which was pinned at import time
  854. *
  855. * This is the cleanup functions which GEM drivers need to call when they use
  856. * @drm_gem_prime_import to import dma-bufs.
  857. */
  858. void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
  859. {
  860. struct dma_buf_attachment *attach;
  861. struct dma_buf *dma_buf;
  862. attach = obj->import_attach;
  863. if (sg)
  864. dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
  865. dma_buf = attach->dmabuf;
  866. dma_buf_detach(attach->dmabuf, attach);
  867. /* remove the reference */
  868. dma_buf_put(dma_buf);
  869. }
  870. EXPORT_SYMBOL(drm_prime_gem_destroy);
  871. void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
  872. {
  873. mutex_init(&prime_fpriv->lock);
  874. prime_fpriv->dmabufs = RB_ROOT;
  875. prime_fpriv->handles = RB_ROOT;
  876. }
  877. void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
  878. {
  879. /* by now drm_gem_release should've made sure the list is empty */
  880. WARN_ON(!RB_EMPTY_ROOT(&prime_fpriv->dmabufs));
  881. }