drm_prime.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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 <linux/module.h>
  32. #include <drm/drm.h>
  33. #include <drm/drm_drv.h>
  34. #include <drm/drm_file.h>
  35. #include <drm/drm_framebuffer.h>
  36. #include <drm/drm_gem.h>
  37. #include <drm/drm_prime.h>
  38. #include "drm_internal.h"
  39. MODULE_IMPORT_NS(DMA_BUF);
  40. /**
  41. * DOC: overview and lifetime rules
  42. *
  43. * Similar to GEM global names, PRIME file descriptors are also used to share
  44. * buffer objects across processes. They offer additional security: as file
  45. * descriptors must be explicitly sent over UNIX domain sockets to be shared
  46. * between applications, they can't be guessed like the globally unique GEM
  47. * names.
  48. *
  49. * Drivers that support the PRIME API implement the drm_gem_object_funcs.export
  50. * and &drm_driver.gem_prime_import hooks. &dma_buf_ops implementations for
  51. * drivers are all individually exported for drivers which need to overwrite
  52. * or reimplement some of them.
  53. *
  54. * Reference Counting for GEM Drivers
  55. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  56. *
  57. * On the export the &dma_buf holds a reference to the exported buffer object,
  58. * usually a &drm_gem_object. It takes this reference in the PRIME_HANDLE_TO_FD
  59. * IOCTL, when it first calls &drm_gem_object_funcs.export
  60. * and stores the exporting GEM object in the &dma_buf.priv field. This
  61. * reference needs to be released when the final reference to the &dma_buf
  62. * itself is dropped and its &dma_buf_ops.release function is called. For
  63. * GEM-based drivers, the &dma_buf should be exported using
  64. * drm_gem_dmabuf_export() and then released by drm_gem_dmabuf_release().
  65. *
  66. * Thus the chain of references always flows in one direction, avoiding loops:
  67. * importing GEM object -> dma-buf -> exported GEM bo. A further complication
  68. * are the lookup caches for import and export. These are required to guarantee
  69. * that any given object will always have only one unique userspace handle. This
  70. * is required to allow userspace to detect duplicated imports, since some GEM
  71. * drivers do fail command submissions if a given buffer object is listed more
  72. * than once. These import and export caches in &drm_prime_file_private only
  73. * retain a weak reference, which is cleaned up when the corresponding object is
  74. * released.
  75. *
  76. * Self-importing: If userspace is using PRIME as a replacement for flink then
  77. * it will get a fd->handle request for a GEM object that it created. Drivers
  78. * should detect this situation and return back the underlying object from the
  79. * dma-buf private. For GEM based drivers this is handled in
  80. * drm_gem_prime_import() already.
  81. */
  82. struct drm_prime_member {
  83. struct dma_buf *dma_buf;
  84. uint32_t handle;
  85. struct rb_node dmabuf_rb;
  86. struct rb_node handle_rb;
  87. };
  88. static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
  89. struct dma_buf *dma_buf, uint32_t handle)
  90. {
  91. struct drm_prime_member *member;
  92. struct rb_node **p, *rb;
  93. member = kmalloc(sizeof(*member), GFP_KERNEL);
  94. if (!member)
  95. return -ENOMEM;
  96. get_dma_buf(dma_buf);
  97. member->dma_buf = dma_buf;
  98. member->handle = handle;
  99. rb = NULL;
  100. p = &prime_fpriv->dmabufs.rb_node;
  101. while (*p) {
  102. struct drm_prime_member *pos;
  103. rb = *p;
  104. pos = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
  105. if (dma_buf > pos->dma_buf)
  106. p = &rb->rb_right;
  107. else
  108. p = &rb->rb_left;
  109. }
  110. rb_link_node(&member->dmabuf_rb, rb, p);
  111. rb_insert_color(&member->dmabuf_rb, &prime_fpriv->dmabufs);
  112. rb = NULL;
  113. p = &prime_fpriv->handles.rb_node;
  114. while (*p) {
  115. struct drm_prime_member *pos;
  116. rb = *p;
  117. pos = rb_entry(rb, struct drm_prime_member, handle_rb);
  118. if (handle > pos->handle)
  119. p = &rb->rb_right;
  120. else
  121. p = &rb->rb_left;
  122. }
  123. rb_link_node(&member->handle_rb, rb, p);
  124. rb_insert_color(&member->handle_rb, &prime_fpriv->handles);
  125. return 0;
  126. }
  127. static struct dma_buf *drm_prime_lookup_buf_by_handle(struct drm_prime_file_private *prime_fpriv,
  128. uint32_t handle)
  129. {
  130. struct rb_node *rb;
  131. rb = prime_fpriv->handles.rb_node;
  132. while (rb) {
  133. struct drm_prime_member *member;
  134. member = rb_entry(rb, struct drm_prime_member, handle_rb);
  135. if (member->handle == handle)
  136. return member->dma_buf;
  137. else if (member->handle < handle)
  138. rb = rb->rb_right;
  139. else
  140. rb = rb->rb_left;
  141. }
  142. return NULL;
  143. }
  144. static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv,
  145. struct dma_buf *dma_buf,
  146. uint32_t *handle)
  147. {
  148. struct rb_node *rb;
  149. rb = prime_fpriv->dmabufs.rb_node;
  150. while (rb) {
  151. struct drm_prime_member *member;
  152. member = rb_entry(rb, struct drm_prime_member, dmabuf_rb);
  153. if (member->dma_buf == dma_buf) {
  154. *handle = member->handle;
  155. return 0;
  156. } else if (member->dma_buf < dma_buf) {
  157. rb = rb->rb_right;
  158. } else {
  159. rb = rb->rb_left;
  160. }
  161. }
  162. return -ENOENT;
  163. }
  164. void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv,
  165. uint32_t handle)
  166. {
  167. struct rb_node *rb;
  168. mutex_lock(&prime_fpriv->lock);
  169. rb = prime_fpriv->handles.rb_node;
  170. while (rb) {
  171. struct drm_prime_member *member;
  172. member = rb_entry(rb, struct drm_prime_member, handle_rb);
  173. if (member->handle == handle) {
  174. rb_erase(&member->handle_rb, &prime_fpriv->handles);
  175. rb_erase(&member->dmabuf_rb, &prime_fpriv->dmabufs);
  176. dma_buf_put(member->dma_buf);
  177. kfree(member);
  178. break;
  179. } else if (member->handle < handle) {
  180. rb = rb->rb_right;
  181. } else {
  182. rb = rb->rb_left;
  183. }
  184. }
  185. mutex_unlock(&prime_fpriv->lock);
  186. }
  187. void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
  188. {
  189. mutex_init(&prime_fpriv->lock);
  190. prime_fpriv->dmabufs = RB_ROOT;
  191. prime_fpriv->handles = RB_ROOT;
  192. }
  193. void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
  194. {
  195. /* by now drm_gem_release should've made sure the list is empty */
  196. WARN_ON(!RB_EMPTY_ROOT(&prime_fpriv->dmabufs));
  197. }
  198. /**
  199. * drm_gem_dmabuf_export - &dma_buf export implementation for GEM
  200. * @dev: parent device for the exported dmabuf
  201. * @exp_info: the export information used by dma_buf_export()
  202. *
  203. * This wraps dma_buf_export() for use by generic GEM drivers that are using
  204. * drm_gem_dmabuf_release(). In addition to calling dma_buf_export(), we take
  205. * a reference to the &drm_device and the exported &drm_gem_object (stored in
  206. * &dma_buf_export_info.priv) which is released by drm_gem_dmabuf_release().
  207. *
  208. * Returns the new dmabuf.
  209. */
  210. struct dma_buf *drm_gem_dmabuf_export(struct drm_device *dev,
  211. struct dma_buf_export_info *exp_info)
  212. {
  213. struct drm_gem_object *obj = exp_info->priv;
  214. struct dma_buf *dma_buf;
  215. dma_buf = dma_buf_export(exp_info);
  216. if (IS_ERR(dma_buf))
  217. return dma_buf;
  218. drm_dev_get(dev);
  219. drm_gem_object_get(obj);
  220. dma_buf->file->f_mapping = obj->dev->anon_inode->i_mapping;
  221. return dma_buf;
  222. }
  223. EXPORT_SYMBOL(drm_gem_dmabuf_export);
  224. /**
  225. * drm_gem_dmabuf_release - &dma_buf release implementation for GEM
  226. * @dma_buf: buffer to be released
  227. *
  228. * Generic release function for dma_bufs exported as PRIME buffers. GEM drivers
  229. * must use this in their &dma_buf_ops structure as the release callback.
  230. * drm_gem_dmabuf_release() should be used in conjunction with
  231. * drm_gem_dmabuf_export().
  232. */
  233. void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
  234. {
  235. struct drm_gem_object *obj = dma_buf->priv;
  236. struct drm_device *dev = obj->dev;
  237. /* drop the reference on the export fd holds */
  238. drm_gem_object_put(obj);
  239. drm_dev_put(dev);
  240. }
  241. EXPORT_SYMBOL(drm_gem_dmabuf_release);
  242. /**
  243. * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers
  244. * @dev: drm_device to import into
  245. * @file_priv: drm file-private structure
  246. * @prime_fd: fd id of the dma-buf which should be imported
  247. * @handle: pointer to storage for the handle of the imported buffer object
  248. *
  249. * This is the PRIME import function which must be used mandatorily by GEM
  250. * drivers to ensure correct lifetime management of the underlying GEM object.
  251. * The actual importing of GEM object from the dma-buf is done through the
  252. * &drm_driver.gem_prime_import driver callback.
  253. *
  254. * Returns 0 on success or a negative error code on failure.
  255. */
  256. int drm_gem_prime_fd_to_handle(struct drm_device *dev,
  257. struct drm_file *file_priv, int prime_fd,
  258. uint32_t *handle)
  259. {
  260. struct dma_buf *dma_buf;
  261. struct drm_gem_object *obj;
  262. int ret;
  263. dma_buf = dma_buf_get(prime_fd);
  264. if (IS_ERR(dma_buf))
  265. return PTR_ERR(dma_buf);
  266. mutex_lock(&file_priv->prime.lock);
  267. ret = drm_prime_lookup_buf_handle(&file_priv->prime,
  268. dma_buf, handle);
  269. if (ret == 0)
  270. goto out_put;
  271. /* never seen this one, need to import */
  272. mutex_lock(&dev->object_name_lock);
  273. if (dev->driver->gem_prime_import)
  274. obj = dev->driver->gem_prime_import(dev, dma_buf);
  275. else
  276. obj = drm_gem_prime_import(dev, dma_buf);
  277. if (IS_ERR(obj)) {
  278. ret = PTR_ERR(obj);
  279. goto out_unlock;
  280. }
  281. if (obj->dma_buf) {
  282. WARN_ON(obj->dma_buf != dma_buf);
  283. } else {
  284. obj->dma_buf = dma_buf;
  285. get_dma_buf(dma_buf);
  286. }
  287. /* _handle_create_tail unconditionally unlocks dev->object_name_lock. */
  288. ret = drm_gem_handle_create_tail(file_priv, obj, handle);
  289. drm_gem_object_put(obj);
  290. if (ret)
  291. goto out_put;
  292. ret = drm_prime_add_buf_handle(&file_priv->prime,
  293. dma_buf, *handle);
  294. mutex_unlock(&file_priv->prime.lock);
  295. if (ret)
  296. goto fail;
  297. dma_buf_put(dma_buf);
  298. return 0;
  299. fail:
  300. /* hmm, if driver attached, we are relying on the free-object path
  301. * to detach.. which seems ok..
  302. */
  303. drm_gem_handle_delete(file_priv, *handle);
  304. dma_buf_put(dma_buf);
  305. return ret;
  306. out_unlock:
  307. mutex_unlock(&dev->object_name_lock);
  308. out_put:
  309. mutex_unlock(&file_priv->prime.lock);
  310. dma_buf_put(dma_buf);
  311. return ret;
  312. }
  313. EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
  314. int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
  315. struct drm_file *file_priv)
  316. {
  317. struct drm_prime_handle *args = data;
  318. if (dev->driver->prime_fd_to_handle) {
  319. return dev->driver->prime_fd_to_handle(dev, file_priv, args->fd,
  320. &args->handle);
  321. }
  322. return drm_gem_prime_fd_to_handle(dev, file_priv, args->fd, &args->handle);
  323. }
  324. static struct dma_buf *export_and_register_object(struct drm_device *dev,
  325. struct drm_gem_object *obj,
  326. uint32_t flags)
  327. {
  328. struct dma_buf *dmabuf;
  329. /* prevent races with concurrent gem_close. */
  330. if (obj->handle_count == 0) {
  331. dmabuf = ERR_PTR(-ENOENT);
  332. return dmabuf;
  333. }
  334. if (obj->funcs && obj->funcs->export)
  335. dmabuf = obj->funcs->export(obj, flags);
  336. else
  337. dmabuf = drm_gem_prime_export(obj, flags);
  338. if (IS_ERR(dmabuf)) {
  339. /* normally the created dma-buf takes ownership of the ref,
  340. * but if that fails then drop the ref
  341. */
  342. return dmabuf;
  343. }
  344. /*
  345. * Note that callers do not need to clean up the export cache
  346. * since the check for obj->handle_count guarantees that someone
  347. * will clean it up.
  348. */
  349. obj->dma_buf = dmabuf;
  350. get_dma_buf(obj->dma_buf);
  351. return dmabuf;
  352. }
  353. /**
  354. * drm_gem_prime_handle_to_dmabuf - PRIME export function for GEM drivers
  355. * @dev: dev to export the buffer from
  356. * @file_priv: drm file-private structure
  357. * @handle: buffer handle to export
  358. * @flags: flags like DRM_CLOEXEC
  359. *
  360. * This is the PRIME export function which must be used mandatorily by GEM
  361. * drivers to ensure correct lifetime management of the underlying GEM object.
  362. * The actual exporting from GEM object to a dma-buf is done through the
  363. * &drm_gem_object_funcs.export callback.
  364. *
  365. * Unlike drm_gem_prime_handle_to_fd(), it returns the struct dma_buf it
  366. * has created, without attaching it to any file descriptors. The difference
  367. * between those two is similar to that between anon_inode_getfile() and
  368. * anon_inode_getfd(); insertion into descriptor table is something you
  369. * can not revert if any cleanup is needed, so the descriptor-returning
  370. * variants should only be used when you are past the last failure exit
  371. * and the only thing left is passing the new file descriptor to userland.
  372. * When all you need is the object itself or when you need to do something
  373. * else that might fail, use that one instead.
  374. */
  375. struct dma_buf *drm_gem_prime_handle_to_dmabuf(struct drm_device *dev,
  376. struct drm_file *file_priv, uint32_t handle,
  377. uint32_t flags)
  378. {
  379. struct drm_gem_object *obj;
  380. int ret = 0;
  381. struct dma_buf *dmabuf;
  382. mutex_lock(&file_priv->prime.lock);
  383. obj = drm_gem_object_lookup(file_priv, handle);
  384. if (!obj) {
  385. dmabuf = ERR_PTR(-ENOENT);
  386. goto out_unlock;
  387. }
  388. dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle);
  389. if (dmabuf) {
  390. get_dma_buf(dmabuf);
  391. goto out;
  392. }
  393. mutex_lock(&dev->object_name_lock);
  394. /* re-export the original imported object */
  395. if (obj->import_attach) {
  396. dmabuf = obj->import_attach->dmabuf;
  397. get_dma_buf(dmabuf);
  398. goto out_have_obj;
  399. }
  400. if (obj->dma_buf) {
  401. get_dma_buf(obj->dma_buf);
  402. dmabuf = obj->dma_buf;
  403. goto out_have_obj;
  404. }
  405. dmabuf = export_and_register_object(dev, obj, flags);
  406. if (IS_ERR(dmabuf)) {
  407. /* normally the created dma-buf takes ownership of the ref,
  408. * but if that fails then drop the ref
  409. */
  410. mutex_unlock(&dev->object_name_lock);
  411. goto out;
  412. }
  413. out_have_obj:
  414. /*
  415. * If we've exported this buffer then cheat and add it to the import list
  416. * so we get the correct handle back. We must do this under the
  417. * protection of dev->object_name_lock to ensure that a racing gem close
  418. * ioctl doesn't miss to remove this buffer handle from the cache.
  419. */
  420. ret = drm_prime_add_buf_handle(&file_priv->prime,
  421. dmabuf, handle);
  422. mutex_unlock(&dev->object_name_lock);
  423. if (ret) {
  424. dma_buf_put(dmabuf);
  425. dmabuf = ERR_PTR(ret);
  426. }
  427. out:
  428. drm_gem_object_put(obj);
  429. out_unlock:
  430. mutex_unlock(&file_priv->prime.lock);
  431. return dmabuf;
  432. }
  433. EXPORT_SYMBOL(drm_gem_prime_handle_to_dmabuf);
  434. /**
  435. * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers
  436. * @dev: dev to export the buffer from
  437. * @file_priv: drm file-private structure
  438. * @handle: buffer handle to export
  439. * @flags: flags like DRM_CLOEXEC
  440. * @prime_fd: pointer to storage for the fd id of the create dma-buf
  441. *
  442. * This is the PRIME export function which must be used mandatorily by GEM
  443. * drivers to ensure correct lifetime management of the underlying GEM object.
  444. * The actual exporting from GEM object to a dma-buf is done through the
  445. * &drm_gem_object_funcs.export callback.
  446. */
  447. int drm_gem_prime_handle_to_fd(struct drm_device *dev,
  448. struct drm_file *file_priv, uint32_t handle,
  449. uint32_t flags,
  450. int *prime_fd)
  451. {
  452. struct dma_buf *dmabuf;
  453. int fd = get_unused_fd_flags(flags);
  454. if (fd < 0)
  455. return fd;
  456. dmabuf = drm_gem_prime_handle_to_dmabuf(dev, file_priv, handle, flags);
  457. if (IS_ERR(dmabuf)) {
  458. put_unused_fd(fd);
  459. return PTR_ERR(dmabuf);
  460. }
  461. fd_install(fd, dmabuf->file);
  462. *prime_fd = fd;
  463. return 0;
  464. }
  465. EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
  466. int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
  467. struct drm_file *file_priv)
  468. {
  469. struct drm_prime_handle *args = data;
  470. /* check flags are valid */
  471. if (args->flags & ~(DRM_CLOEXEC | DRM_RDWR))
  472. return -EINVAL;
  473. if (dev->driver->prime_handle_to_fd) {
  474. return dev->driver->prime_handle_to_fd(dev, file_priv,
  475. args->handle, args->flags,
  476. &args->fd);
  477. }
  478. return drm_gem_prime_handle_to_fd(dev, file_priv, args->handle,
  479. args->flags, &args->fd);
  480. }
  481. /**
  482. * DOC: PRIME Helpers
  483. *
  484. * Drivers can implement &drm_gem_object_funcs.export and
  485. * &drm_driver.gem_prime_import in terms of simpler APIs by using the helper
  486. * functions drm_gem_prime_export() and drm_gem_prime_import(). These functions
  487. * implement dma-buf support in terms of some lower-level helpers, which are
  488. * again exported for drivers to use individually:
  489. *
  490. * Exporting buffers
  491. * ~~~~~~~~~~~~~~~~~
  492. *
  493. * Optional pinning of buffers is handled at dma-buf attach and detach time in
  494. * drm_gem_map_attach() and drm_gem_map_detach(). Backing storage itself is
  495. * handled by drm_gem_map_dma_buf() and drm_gem_unmap_dma_buf(), which relies on
  496. * &drm_gem_object_funcs.get_sg_table. If &drm_gem_object_funcs.get_sg_table is
  497. * unimplemented, exports into another device are rejected.
  498. *
  499. * For kernel-internal access there's drm_gem_dmabuf_vmap() and
  500. * drm_gem_dmabuf_vunmap(). Userspace mmap support is provided by
  501. * drm_gem_dmabuf_mmap().
  502. *
  503. * Note that these export helpers can only be used if the underlying backing
  504. * storage is fully coherent and either permanently pinned, or it is safe to pin
  505. * it indefinitely.
  506. *
  507. * FIXME: The underlying helper functions are named rather inconsistently.
  508. *
  509. * Importing buffers
  510. * ~~~~~~~~~~~~~~~~~
  511. *
  512. * Importing dma-bufs using drm_gem_prime_import() relies on
  513. * &drm_driver.gem_prime_import_sg_table.
  514. *
  515. * Note that similarly to the export helpers this permanently pins the
  516. * underlying backing storage. Which is ok for scanout, but is not the best
  517. * option for sharing lots of buffers for rendering.
  518. */
  519. /**
  520. * drm_gem_map_attach - dma_buf attach implementation for GEM
  521. * @dma_buf: buffer to attach device to
  522. * @attach: buffer attachment data
  523. *
  524. * Calls &drm_gem_object_funcs.pin for device specific handling. This can be
  525. * used as the &dma_buf_ops.attach callback. Must be used together with
  526. * drm_gem_map_detach().
  527. *
  528. * Returns 0 on success, negative error code on failure.
  529. */
  530. int drm_gem_map_attach(struct dma_buf *dma_buf,
  531. struct dma_buf_attachment *attach)
  532. {
  533. struct drm_gem_object *obj = dma_buf->priv;
  534. /*
  535. * drm_gem_map_dma_buf() requires obj->get_sg_table(), but drivers
  536. * that implement their own ->map_dma_buf() do not.
  537. */
  538. if (dma_buf->ops->map_dma_buf == drm_gem_map_dma_buf &&
  539. !obj->funcs->get_sg_table)
  540. return -ENOSYS;
  541. return drm_gem_pin(obj);
  542. }
  543. EXPORT_SYMBOL(drm_gem_map_attach);
  544. /**
  545. * drm_gem_map_detach - dma_buf detach implementation for GEM
  546. * @dma_buf: buffer to detach from
  547. * @attach: attachment to be detached
  548. *
  549. * Calls &drm_gem_object_funcs.pin for device specific handling. Cleans up
  550. * &dma_buf_attachment from drm_gem_map_attach(). This can be used as the
  551. * &dma_buf_ops.detach callback.
  552. */
  553. void drm_gem_map_detach(struct dma_buf *dma_buf,
  554. struct dma_buf_attachment *attach)
  555. {
  556. struct drm_gem_object *obj = dma_buf->priv;
  557. drm_gem_unpin(obj);
  558. }
  559. EXPORT_SYMBOL(drm_gem_map_detach);
  560. /**
  561. * drm_gem_map_dma_buf - map_dma_buf implementation for GEM
  562. * @attach: attachment whose scatterlist is to be returned
  563. * @dir: direction of DMA transfer
  564. *
  565. * Calls &drm_gem_object_funcs.get_sg_table and then maps the scatterlist. This
  566. * can be used as the &dma_buf_ops.map_dma_buf callback. Should be used together
  567. * with drm_gem_unmap_dma_buf().
  568. *
  569. * Returns:sg_table containing the scatterlist to be returned; returns ERR_PTR
  570. * on error. May return -EINTR if it is interrupted by a signal.
  571. */
  572. struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
  573. enum dma_data_direction dir)
  574. {
  575. struct drm_gem_object *obj = attach->dmabuf->priv;
  576. struct sg_table *sgt;
  577. int ret;
  578. if (WARN_ON(dir == DMA_NONE))
  579. return ERR_PTR(-EINVAL);
  580. if (WARN_ON(!obj->funcs->get_sg_table))
  581. return ERR_PTR(-ENOSYS);
  582. sgt = obj->funcs->get_sg_table(obj);
  583. if (IS_ERR(sgt))
  584. return sgt;
  585. ret = dma_map_sgtable(attach->dev, sgt, dir,
  586. DMA_ATTR_SKIP_CPU_SYNC);
  587. if (ret) {
  588. sg_free_table(sgt);
  589. kfree(sgt);
  590. sgt = ERR_PTR(ret);
  591. }
  592. return sgt;
  593. }
  594. EXPORT_SYMBOL(drm_gem_map_dma_buf);
  595. /**
  596. * drm_gem_unmap_dma_buf - unmap_dma_buf implementation for GEM
  597. * @attach: attachment to unmap buffer from
  598. * @sgt: scatterlist info of the buffer to unmap
  599. * @dir: direction of DMA transfer
  600. *
  601. * This can be used as the &dma_buf_ops.unmap_dma_buf callback.
  602. */
  603. void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
  604. struct sg_table *sgt,
  605. enum dma_data_direction dir)
  606. {
  607. if (!sgt)
  608. return;
  609. dma_unmap_sgtable(attach->dev, sgt, dir, DMA_ATTR_SKIP_CPU_SYNC);
  610. sg_free_table(sgt);
  611. kfree(sgt);
  612. }
  613. EXPORT_SYMBOL(drm_gem_unmap_dma_buf);
  614. /**
  615. * drm_gem_dmabuf_vmap - dma_buf vmap implementation for GEM
  616. * @dma_buf: buffer to be mapped
  617. * @map: the virtual address of the buffer
  618. *
  619. * Sets up a kernel virtual mapping. This can be used as the &dma_buf_ops.vmap
  620. * callback. Calls into &drm_gem_object_funcs.vmap for device specific handling.
  621. * The kernel virtual address is returned in map.
  622. *
  623. * Returns 0 on success or a negative errno code otherwise.
  624. */
  625. int drm_gem_dmabuf_vmap(struct dma_buf *dma_buf, struct iosys_map *map)
  626. {
  627. struct drm_gem_object *obj = dma_buf->priv;
  628. return drm_gem_vmap(obj, map);
  629. }
  630. EXPORT_SYMBOL(drm_gem_dmabuf_vmap);
  631. /**
  632. * drm_gem_dmabuf_vunmap - dma_buf vunmap implementation for GEM
  633. * @dma_buf: buffer to be unmapped
  634. * @map: the virtual address of the buffer
  635. *
  636. * Releases a kernel virtual mapping. This can be used as the
  637. * &dma_buf_ops.vunmap callback. Calls into &drm_gem_object_funcs.vunmap for device specific handling.
  638. */
  639. void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, struct iosys_map *map)
  640. {
  641. struct drm_gem_object *obj = dma_buf->priv;
  642. drm_gem_vunmap(obj, map);
  643. }
  644. EXPORT_SYMBOL(drm_gem_dmabuf_vunmap);
  645. /**
  646. * drm_gem_prime_mmap - PRIME mmap function for GEM drivers
  647. * @obj: GEM object
  648. * @vma: Virtual address range
  649. *
  650. * This function sets up a userspace mapping for PRIME exported buffers using
  651. * the same codepath that is used for regular GEM buffer mapping on the DRM fd.
  652. * The fake GEM offset is added to vma->vm_pgoff and &drm_driver->fops->mmap is
  653. * called to set up the mapping.
  654. */
  655. int drm_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
  656. {
  657. struct drm_file *priv;
  658. struct file *fil;
  659. int ret;
  660. /* Add the fake offset */
  661. vma->vm_pgoff += drm_vma_node_start(&obj->vma_node);
  662. if (obj->funcs && obj->funcs->mmap) {
  663. vma->vm_ops = obj->funcs->vm_ops;
  664. drm_gem_object_get(obj);
  665. ret = obj->funcs->mmap(obj, vma);
  666. if (ret) {
  667. drm_gem_object_put(obj);
  668. return ret;
  669. }
  670. vma->vm_private_data = obj;
  671. return 0;
  672. }
  673. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  674. fil = kzalloc(sizeof(*fil), GFP_KERNEL);
  675. if (!priv || !fil) {
  676. ret = -ENOMEM;
  677. goto out;
  678. }
  679. /* Used by drm_gem_mmap() to lookup the GEM object */
  680. priv->minor = obj->dev->primary;
  681. fil->private_data = priv;
  682. ret = drm_vma_node_allow(&obj->vma_node, priv);
  683. if (ret)
  684. goto out;
  685. ret = obj->dev->driver->fops->mmap(fil, vma);
  686. drm_vma_node_revoke(&obj->vma_node, priv);
  687. out:
  688. kfree(priv);
  689. kfree(fil);
  690. return ret;
  691. }
  692. EXPORT_SYMBOL(drm_gem_prime_mmap);
  693. /**
  694. * drm_gem_dmabuf_mmap - dma_buf mmap implementation for GEM
  695. * @dma_buf: buffer to be mapped
  696. * @vma: virtual address range
  697. *
  698. * Provides memory mapping for the buffer. This can be used as the
  699. * &dma_buf_ops.mmap callback. It just forwards to drm_gem_prime_mmap().
  700. *
  701. * Returns 0 on success or a negative error code on failure.
  702. */
  703. int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
  704. {
  705. struct drm_gem_object *obj = dma_buf->priv;
  706. return drm_gem_prime_mmap(obj, vma);
  707. }
  708. EXPORT_SYMBOL(drm_gem_dmabuf_mmap);
  709. static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
  710. .cache_sgt_mapping = true,
  711. .attach = drm_gem_map_attach,
  712. .detach = drm_gem_map_detach,
  713. .map_dma_buf = drm_gem_map_dma_buf,
  714. .unmap_dma_buf = drm_gem_unmap_dma_buf,
  715. .release = drm_gem_dmabuf_release,
  716. .mmap = drm_gem_dmabuf_mmap,
  717. .vmap = drm_gem_dmabuf_vmap,
  718. .vunmap = drm_gem_dmabuf_vunmap,
  719. };
  720. /**
  721. * drm_prime_pages_to_sg - converts a page array into an sg list
  722. * @dev: DRM device
  723. * @pages: pointer to the array of page pointers to convert
  724. * @nr_pages: length of the page vector
  725. *
  726. * This helper creates an sg table object from a set of pages
  727. * the driver is responsible for mapping the pages into the
  728. * importers address space for use with dma_buf itself.
  729. *
  730. * This is useful for implementing &drm_gem_object_funcs.get_sg_table.
  731. */
  732. struct sg_table *drm_prime_pages_to_sg(struct drm_device *dev,
  733. struct page **pages, unsigned int nr_pages)
  734. {
  735. struct sg_table *sg;
  736. size_t max_segment = 0;
  737. int err;
  738. sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
  739. if (!sg)
  740. return ERR_PTR(-ENOMEM);
  741. if (dev)
  742. max_segment = dma_max_mapping_size(dev->dev);
  743. if (max_segment == 0)
  744. max_segment = UINT_MAX;
  745. err = sg_alloc_table_from_pages_segment(sg, pages, nr_pages, 0,
  746. (unsigned long)nr_pages << PAGE_SHIFT,
  747. max_segment, GFP_KERNEL);
  748. if (err) {
  749. kfree(sg);
  750. sg = ERR_PTR(err);
  751. }
  752. return sg;
  753. }
  754. EXPORT_SYMBOL(drm_prime_pages_to_sg);
  755. /**
  756. * drm_prime_get_contiguous_size - returns the contiguous size of the buffer
  757. * @sgt: sg_table describing the buffer to check
  758. *
  759. * This helper calculates the contiguous size in the DMA address space
  760. * of the buffer described by the provided sg_table.
  761. *
  762. * This is useful for implementing
  763. * &drm_gem_object_funcs.gem_prime_import_sg_table.
  764. */
  765. unsigned long drm_prime_get_contiguous_size(struct sg_table *sgt)
  766. {
  767. dma_addr_t expected = sg_dma_address(sgt->sgl);
  768. struct scatterlist *sg;
  769. unsigned long size = 0;
  770. int i;
  771. for_each_sgtable_dma_sg(sgt, sg, i) {
  772. unsigned int len = sg_dma_len(sg);
  773. if (!len)
  774. break;
  775. if (sg_dma_address(sg) != expected)
  776. break;
  777. expected += len;
  778. size += len;
  779. }
  780. return size;
  781. }
  782. EXPORT_SYMBOL(drm_prime_get_contiguous_size);
  783. /**
  784. * drm_gem_prime_export - helper library implementation of the export callback
  785. * @obj: GEM object to export
  786. * @flags: flags like DRM_CLOEXEC and DRM_RDWR
  787. *
  788. * This is the implementation of the &drm_gem_object_funcs.export functions for GEM drivers
  789. * using the PRIME helpers. It is used as the default in
  790. * drm_gem_prime_handle_to_fd().
  791. */
  792. struct dma_buf *drm_gem_prime_export(struct drm_gem_object *obj,
  793. int flags)
  794. {
  795. struct drm_device *dev = obj->dev;
  796. struct dma_buf_export_info exp_info = {
  797. .exp_name = KBUILD_MODNAME, /* white lie for debug */
  798. .owner = dev->driver->fops->owner,
  799. .ops = &drm_gem_prime_dmabuf_ops,
  800. .size = obj->size,
  801. .flags = flags,
  802. .priv = obj,
  803. .resv = obj->resv,
  804. };
  805. return drm_gem_dmabuf_export(dev, &exp_info);
  806. }
  807. EXPORT_SYMBOL(drm_gem_prime_export);
  808. /**
  809. * drm_gem_prime_import_dev - core implementation of the import callback
  810. * @dev: drm_device to import into
  811. * @dma_buf: dma-buf object to import
  812. * @attach_dev: struct device to dma_buf attach
  813. *
  814. * This is the core of drm_gem_prime_import(). It's designed to be called by
  815. * drivers who want to use a different device structure than &drm_device.dev for
  816. * attaching via dma_buf. This function calls
  817. * &drm_driver.gem_prime_import_sg_table internally.
  818. *
  819. * Drivers must arrange to call drm_prime_gem_destroy() from their
  820. * &drm_gem_object_funcs.free hook when using this function.
  821. */
  822. struct drm_gem_object *drm_gem_prime_import_dev(struct drm_device *dev,
  823. struct dma_buf *dma_buf,
  824. struct device *attach_dev)
  825. {
  826. struct dma_buf_attachment *attach;
  827. struct sg_table *sgt;
  828. struct drm_gem_object *obj;
  829. int ret;
  830. if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
  831. obj = dma_buf->priv;
  832. if (obj->dev == dev) {
  833. /*
  834. * Importing dmabuf exported from our own gem increases
  835. * refcount on gem itself instead of f_count of dmabuf.
  836. */
  837. drm_gem_object_get(obj);
  838. return obj;
  839. }
  840. }
  841. if (!dev->driver->gem_prime_import_sg_table)
  842. return ERR_PTR(-EINVAL);
  843. attach = dma_buf_attach(dma_buf, attach_dev);
  844. if (IS_ERR(attach))
  845. return ERR_CAST(attach);
  846. get_dma_buf(dma_buf);
  847. sgt = dma_buf_map_attachment_unlocked(attach, DMA_BIDIRECTIONAL);
  848. if (IS_ERR(sgt)) {
  849. ret = PTR_ERR(sgt);
  850. goto fail_detach;
  851. }
  852. obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
  853. if (IS_ERR(obj)) {
  854. ret = PTR_ERR(obj);
  855. goto fail_unmap;
  856. }
  857. obj->import_attach = attach;
  858. obj->resv = dma_buf->resv;
  859. return obj;
  860. fail_unmap:
  861. dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_BIDIRECTIONAL);
  862. fail_detach:
  863. dma_buf_detach(dma_buf, attach);
  864. dma_buf_put(dma_buf);
  865. return ERR_PTR(ret);
  866. }
  867. EXPORT_SYMBOL(drm_gem_prime_import_dev);
  868. /**
  869. * drm_gem_prime_import - helper library implementation of the import callback
  870. * @dev: drm_device to import into
  871. * @dma_buf: dma-buf object to import
  872. *
  873. * This is the implementation of the gem_prime_import functions for GEM drivers
  874. * using the PRIME helpers. Drivers can use this as their
  875. * &drm_driver.gem_prime_import implementation. It is used as the default
  876. * implementation in drm_gem_prime_fd_to_handle().
  877. *
  878. * Drivers must arrange to call drm_prime_gem_destroy() from their
  879. * &drm_gem_object_funcs.free hook when using this function.
  880. */
  881. struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
  882. struct dma_buf *dma_buf)
  883. {
  884. return drm_gem_prime_import_dev(dev, dma_buf, dev->dev);
  885. }
  886. EXPORT_SYMBOL(drm_gem_prime_import);
  887. /**
  888. * drm_prime_sg_to_page_array - convert an sg table into a page array
  889. * @sgt: scatter-gather table to convert
  890. * @pages: array of page pointers to store the pages in
  891. * @max_entries: size of the passed-in array
  892. *
  893. * Exports an sg table into an array of pages.
  894. *
  895. * This function is deprecated and strongly discouraged to be used.
  896. * The page array is only useful for page faults and those can corrupt fields
  897. * in the struct page if they are not handled by the exporting driver.
  898. */
  899. int __deprecated drm_prime_sg_to_page_array(struct sg_table *sgt,
  900. struct page **pages,
  901. int max_entries)
  902. {
  903. struct sg_page_iter page_iter;
  904. struct page **p = pages;
  905. for_each_sgtable_page(sgt, &page_iter, 0) {
  906. if (WARN_ON(p - pages >= max_entries))
  907. return -1;
  908. *p++ = sg_page_iter_page(&page_iter);
  909. }
  910. return 0;
  911. }
  912. EXPORT_SYMBOL(drm_prime_sg_to_page_array);
  913. /**
  914. * drm_prime_sg_to_dma_addr_array - convert an sg table into a dma addr array
  915. * @sgt: scatter-gather table to convert
  916. * @addrs: array to store the dma bus address of each page
  917. * @max_entries: size of both the passed-in arrays
  918. *
  919. * Exports an sg table into an array of addresses.
  920. *
  921. * Drivers should use this in their &drm_driver.gem_prime_import_sg_table
  922. * implementation.
  923. */
  924. int drm_prime_sg_to_dma_addr_array(struct sg_table *sgt, dma_addr_t *addrs,
  925. int max_entries)
  926. {
  927. struct sg_dma_page_iter dma_iter;
  928. dma_addr_t *a = addrs;
  929. for_each_sgtable_dma_page(sgt, &dma_iter, 0) {
  930. if (WARN_ON(a - addrs >= max_entries))
  931. return -1;
  932. *a++ = sg_page_iter_dma_address(&dma_iter);
  933. }
  934. return 0;
  935. }
  936. EXPORT_SYMBOL(drm_prime_sg_to_dma_addr_array);
  937. /**
  938. * drm_prime_gem_destroy - helper to clean up a PRIME-imported GEM object
  939. * @obj: GEM object which was created from a dma-buf
  940. * @sg: the sg-table which was pinned at import time
  941. *
  942. * This is the cleanup functions which GEM drivers need to call when they use
  943. * drm_gem_prime_import() or drm_gem_prime_import_dev() to import dma-bufs.
  944. */
  945. void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
  946. {
  947. struct dma_buf_attachment *attach;
  948. struct dma_buf *dma_buf;
  949. attach = obj->import_attach;
  950. if (sg)
  951. dma_buf_unmap_attachment_unlocked(attach, sg, DMA_BIDIRECTIONAL);
  952. dma_buf = attach->dmabuf;
  953. dma_buf_detach(attach->dmabuf, attach);
  954. /* remove the reference */
  955. dma_buf_put(dma_buf);
  956. }
  957. EXPORT_SYMBOL(drm_prime_gem_destroy);