drm_prime.c 24 KB

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