drm_prime.c 21 KB

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