drm_prime.c 23 KB

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