drm_prime.c 32 KB

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