drm_prime.c 20 KB

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