drm_prime.c 24 KB

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