drm_prime.c 30 KB

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