drm_prime.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. * Copyright © 2012 Red Hat
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Dave Airlie <airlied@redhat.com>
  25. * Rob Clark <rob.clark@linaro.org>
  26. *
  27. */
  28. #include <linux/export.h>
  29. #include <linux/dma-buf.h>
  30. #include <drm/drmP.h>
  31. /*
  32. * DMA-BUF/GEM Object references and lifetime overview:
  33. *
  34. * On the export the dma_buf holds a reference to the exporting GEM
  35. * object. It takes this reference in handle_to_fd_ioctl, when it
  36. * first calls .prime_export and stores the exporting GEM object in
  37. * the dma_buf priv. This reference is released when the dma_buf
  38. * object goes away in the driver .release function.
  39. *
  40. * On the import the importing GEM object holds a reference to the
  41. * dma_buf (which in turn holds a ref to the exporting GEM object).
  42. * It takes that reference in the fd_to_handle ioctl.
  43. * It calls dma_buf_get, creates an attachment to it and stores the
  44. * attachment in the GEM object. When this attachment is destroyed
  45. * when the imported object is destroyed, we remove the attachment
  46. * and drop the reference to the dma_buf.
  47. *
  48. * Thus the chain of references always flows in one direction
  49. * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
  50. *
  51. * Self-importing: if userspace is using PRIME as a replacement for flink
  52. * then it will get a fd->handle request for a GEM object that it created.
  53. * Drivers should detect this situation and return back the gem object
  54. * from the dma-buf private. Prime will do this automatically for drivers that
  55. * use the drm_gem_prime_{import,export} helpers.
  56. */
  57. struct drm_prime_member {
  58. struct list_head entry;
  59. struct dma_buf *dma_buf;
  60. uint32_t handle;
  61. };
  62. static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle);
  63. static int drm_gem_map_attach(struct dma_buf *dma_buf,
  64. struct device *target_dev,
  65. struct dma_buf_attachment *attach)
  66. {
  67. struct drm_gem_object *obj = dma_buf->priv;
  68. struct drm_device *dev = obj->dev;
  69. if (!dev->driver->gem_prime_pin)
  70. return 0;
  71. return dev->driver->gem_prime_pin(obj);
  72. }
  73. static void drm_gem_map_detach(struct dma_buf *dma_buf,
  74. struct dma_buf_attachment *attach)
  75. {
  76. struct drm_gem_object *obj = dma_buf->priv;
  77. struct drm_device *dev = obj->dev;
  78. if (dev->driver->gem_prime_unpin)
  79. dev->driver->gem_prime_unpin(obj);
  80. }
  81. static struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
  82. enum dma_data_direction dir)
  83. {
  84. struct drm_gem_object *obj = attach->dmabuf->priv;
  85. struct sg_table *sgt;
  86. mutex_lock(&obj->dev->struct_mutex);
  87. sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
  88. if (!IS_ERR_OR_NULL(sgt))
  89. dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir);
  90. mutex_unlock(&obj->dev->struct_mutex);
  91. return sgt;
  92. }
  93. static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
  94. struct sg_table *sgt, enum dma_data_direction dir)
  95. {
  96. dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
  97. sg_free_table(sgt);
  98. kfree(sgt);
  99. }
  100. static void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
  101. {
  102. struct drm_gem_object *obj = dma_buf->priv;
  103. if (obj->export_dma_buf == dma_buf) {
  104. /* drop the reference on the export fd holds */
  105. obj->export_dma_buf = NULL;
  106. drm_gem_object_unreference_unlocked(obj);
  107. }
  108. }
  109. static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
  110. {
  111. struct drm_gem_object *obj = dma_buf->priv;
  112. struct drm_device *dev = obj->dev;
  113. return dev->driver->gem_prime_vmap(obj);
  114. }
  115. static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
  116. {
  117. struct drm_gem_object *obj = dma_buf->priv;
  118. struct drm_device *dev = obj->dev;
  119. dev->driver->gem_prime_vunmap(obj, vaddr);
  120. }
  121. static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
  122. unsigned long page_num)
  123. {
  124. return NULL;
  125. }
  126. static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
  127. unsigned long page_num, void *addr)
  128. {
  129. }
  130. static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
  131. unsigned long page_num)
  132. {
  133. return NULL;
  134. }
  135. static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
  136. unsigned long page_num, void *addr)
  137. {
  138. }
  139. static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
  140. struct vm_area_struct *vma)
  141. {
  142. return -EINVAL;
  143. }
  144. static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
  145. .attach = drm_gem_map_attach,
  146. .detach = drm_gem_map_detach,
  147. .map_dma_buf = drm_gem_map_dma_buf,
  148. .unmap_dma_buf = drm_gem_unmap_dma_buf,
  149. .release = drm_gem_dmabuf_release,
  150. .kmap = drm_gem_dmabuf_kmap,
  151. .kmap_atomic = drm_gem_dmabuf_kmap_atomic,
  152. .kunmap = drm_gem_dmabuf_kunmap,
  153. .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
  154. .mmap = drm_gem_dmabuf_mmap,
  155. .vmap = drm_gem_dmabuf_vmap,
  156. .vunmap = drm_gem_dmabuf_vunmap,
  157. };
  158. /**
  159. * DOC: PRIME Helpers
  160. *
  161. * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
  162. * simpler APIs by using the helper functions @drm_gem_prime_export and
  163. * @drm_gem_prime_import. These functions implement dma-buf support in terms of
  164. * five lower-level driver callbacks:
  165. *
  166. * Export callbacks:
  167. *
  168. * - @gem_prime_pin (optional): prepare a GEM object for exporting
  169. *
  170. * - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
  171. *
  172. * - @gem_prime_vmap: vmap a buffer exported by your driver
  173. *
  174. * - @gem_prime_vunmap: vunmap a buffer exported by your driver
  175. *
  176. * Import callback:
  177. *
  178. * - @gem_prime_import_sg_table (import): produce a GEM object from another
  179. * driver's scatter/gather table
  180. */
  181. struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
  182. struct drm_gem_object *obj, int flags)
  183. {
  184. return dma_buf_export(obj, &drm_gem_prime_dmabuf_ops, obj->size, flags);
  185. }
  186. EXPORT_SYMBOL(drm_gem_prime_export);
  187. int drm_gem_prime_handle_to_fd(struct drm_device *dev,
  188. struct drm_file *file_priv, uint32_t handle, uint32_t flags,
  189. int *prime_fd)
  190. {
  191. struct drm_gem_object *obj;
  192. void *buf;
  193. int ret = 0;
  194. struct dma_buf *dmabuf;
  195. obj = drm_gem_object_lookup(dev, file_priv, handle);
  196. if (!obj)
  197. return -ENOENT;
  198. mutex_lock(&file_priv->prime.lock);
  199. /* re-export the original imported object */
  200. if (obj->import_attach) {
  201. dmabuf = obj->import_attach->dmabuf;
  202. goto out_have_obj;
  203. }
  204. if (obj->export_dma_buf) {
  205. dmabuf = obj->export_dma_buf;
  206. goto out_have_obj;
  207. }
  208. buf = dev->driver->gem_prime_export(dev, obj, flags);
  209. if (IS_ERR(buf)) {
  210. /* normally the created dma-buf takes ownership of the ref,
  211. * but if that fails then drop the ref
  212. */
  213. ret = PTR_ERR(buf);
  214. goto out;
  215. }
  216. obj->export_dma_buf = buf;
  217. /* if we've exported this buffer the cheat and add it to the import list
  218. * so we get the correct handle back
  219. */
  220. ret = drm_prime_add_buf_handle(&file_priv->prime,
  221. obj->export_dma_buf, handle);
  222. if (ret)
  223. goto out;
  224. *prime_fd = dma_buf_fd(buf, flags);
  225. mutex_unlock(&file_priv->prime.lock);
  226. return 0;
  227. out_have_obj:
  228. get_dma_buf(dmabuf);
  229. *prime_fd = dma_buf_fd(dmabuf, flags);
  230. out:
  231. drm_gem_object_unreference_unlocked(obj);
  232. mutex_unlock(&file_priv->prime.lock);
  233. return ret;
  234. }
  235. EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
  236. struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
  237. struct dma_buf *dma_buf)
  238. {
  239. struct dma_buf_attachment *attach;
  240. struct sg_table *sgt;
  241. struct drm_gem_object *obj;
  242. int ret;
  243. if (!dev->driver->gem_prime_import_sg_table)
  244. return ERR_PTR(-EINVAL);
  245. if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
  246. obj = dma_buf->priv;
  247. if (obj->dev == dev) {
  248. /*
  249. * Importing dmabuf exported from out own gem increases
  250. * refcount on gem itself instead of f_count of dmabuf.
  251. */
  252. drm_gem_object_reference(obj);
  253. return obj;
  254. }
  255. }
  256. attach = dma_buf_attach(dma_buf, dev->dev);
  257. if (IS_ERR(attach))
  258. return ERR_CAST(attach);
  259. get_dma_buf(dma_buf);
  260. sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  261. if (IS_ERR_OR_NULL(sgt)) {
  262. ret = PTR_ERR(sgt);
  263. goto fail_detach;
  264. }
  265. obj = dev->driver->gem_prime_import_sg_table(dev, dma_buf->size, sgt);
  266. if (IS_ERR(obj)) {
  267. ret = PTR_ERR(obj);
  268. goto fail_unmap;
  269. }
  270. obj->import_attach = attach;
  271. return obj;
  272. fail_unmap:
  273. dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
  274. fail_detach:
  275. dma_buf_detach(dma_buf, attach);
  276. dma_buf_put(dma_buf);
  277. return ERR_PTR(ret);
  278. }
  279. EXPORT_SYMBOL(drm_gem_prime_import);
  280. int drm_gem_prime_fd_to_handle(struct drm_device *dev,
  281. struct drm_file *file_priv, int prime_fd, uint32_t *handle)
  282. {
  283. struct dma_buf *dma_buf;
  284. struct drm_gem_object *obj;
  285. int ret;
  286. dma_buf = dma_buf_get(prime_fd);
  287. if (IS_ERR(dma_buf))
  288. return PTR_ERR(dma_buf);
  289. mutex_lock(&file_priv->prime.lock);
  290. ret = drm_prime_lookup_buf_handle(&file_priv->prime,
  291. dma_buf, handle);
  292. if (!ret) {
  293. ret = 0;
  294. goto out_put;
  295. }
  296. /* never seen this one, need to import */
  297. obj = dev->driver->gem_prime_import(dev, dma_buf);
  298. if (IS_ERR(obj)) {
  299. ret = PTR_ERR(obj);
  300. goto out_put;
  301. }
  302. ret = drm_gem_handle_create(file_priv, obj, handle);
  303. drm_gem_object_unreference_unlocked(obj);
  304. if (ret)
  305. goto out_put;
  306. ret = drm_prime_add_buf_handle(&file_priv->prime,
  307. dma_buf, *handle);
  308. if (ret)
  309. goto fail;
  310. mutex_unlock(&file_priv->prime.lock);
  311. dma_buf_put(dma_buf);
  312. return 0;
  313. fail:
  314. /* hmm, if driver attached, we are relying on the free-object path
  315. * to detach.. which seems ok..
  316. */
  317. drm_gem_object_handle_unreference_unlocked(obj);
  318. out_put:
  319. dma_buf_put(dma_buf);
  320. mutex_unlock(&file_priv->prime.lock);
  321. return ret;
  322. }
  323. EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
  324. int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
  325. struct drm_file *file_priv)
  326. {
  327. struct drm_prime_handle *args = data;
  328. uint32_t flags;
  329. if (!drm_core_check_feature(dev, DRIVER_PRIME))
  330. return -EINVAL;
  331. if (!dev->driver->prime_handle_to_fd)
  332. return -ENOSYS;
  333. /* check flags are valid */
  334. if (args->flags & ~DRM_CLOEXEC)
  335. return -EINVAL;
  336. /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
  337. flags = args->flags & DRM_CLOEXEC;
  338. return dev->driver->prime_handle_to_fd(dev, file_priv,
  339. args->handle, flags, &args->fd);
  340. }
  341. int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
  342. struct drm_file *file_priv)
  343. {
  344. struct drm_prime_handle *args = data;
  345. if (!drm_core_check_feature(dev, DRIVER_PRIME))
  346. return -EINVAL;
  347. if (!dev->driver->prime_fd_to_handle)
  348. return -ENOSYS;
  349. return dev->driver->prime_fd_to_handle(dev, file_priv,
  350. args->fd, &args->handle);
  351. }
  352. /*
  353. * drm_prime_pages_to_sg
  354. *
  355. * this helper creates an sg table object from a set of pages
  356. * the driver is responsible for mapping the pages into the
  357. * importers address space
  358. */
  359. struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages)
  360. {
  361. struct sg_table *sg = NULL;
  362. int ret;
  363. sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
  364. if (!sg)
  365. goto out;
  366. ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
  367. nr_pages << PAGE_SHIFT, GFP_KERNEL);
  368. if (ret)
  369. goto out;
  370. return sg;
  371. out:
  372. kfree(sg);
  373. return NULL;
  374. }
  375. EXPORT_SYMBOL(drm_prime_pages_to_sg);
  376. /* export an sg table into an array of pages and addresses
  377. this is currently required by the TTM driver in order to do correct fault
  378. handling */
  379. int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
  380. dma_addr_t *addrs, int max_pages)
  381. {
  382. unsigned count;
  383. struct scatterlist *sg;
  384. struct page *page;
  385. u32 len, offset;
  386. int pg_index;
  387. dma_addr_t addr;
  388. pg_index = 0;
  389. for_each_sg(sgt->sgl, sg, sgt->nents, count) {
  390. len = sg->length;
  391. offset = sg->offset;
  392. page = sg_page(sg);
  393. addr = sg_dma_address(sg);
  394. while (len > 0) {
  395. if (WARN_ON(pg_index >= max_pages))
  396. return -1;
  397. pages[pg_index] = page;
  398. if (addrs)
  399. addrs[pg_index] = addr;
  400. page++;
  401. addr += PAGE_SIZE;
  402. len -= PAGE_SIZE;
  403. pg_index++;
  404. }
  405. }
  406. return 0;
  407. }
  408. EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
  409. /* helper function to cleanup a GEM/prime object */
  410. void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
  411. {
  412. struct dma_buf_attachment *attach;
  413. struct dma_buf *dma_buf;
  414. attach = obj->import_attach;
  415. if (sg)
  416. dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
  417. dma_buf = attach->dmabuf;
  418. dma_buf_detach(attach->dmabuf, attach);
  419. /* remove the reference */
  420. dma_buf_put(dma_buf);
  421. }
  422. EXPORT_SYMBOL(drm_prime_gem_destroy);
  423. void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
  424. {
  425. INIT_LIST_HEAD(&prime_fpriv->head);
  426. mutex_init(&prime_fpriv->lock);
  427. }
  428. EXPORT_SYMBOL(drm_prime_init_file_private);
  429. void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
  430. {
  431. /* by now drm_gem_release should've made sure the list is empty */
  432. WARN_ON(!list_empty(&prime_fpriv->head));
  433. }
  434. EXPORT_SYMBOL(drm_prime_destroy_file_private);
  435. static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle)
  436. {
  437. struct drm_prime_member *member;
  438. member = kmalloc(sizeof(*member), GFP_KERNEL);
  439. if (!member)
  440. return -ENOMEM;
  441. get_dma_buf(dma_buf);
  442. member->dma_buf = dma_buf;
  443. member->handle = handle;
  444. list_add(&member->entry, &prime_fpriv->head);
  445. return 0;
  446. }
  447. int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle)
  448. {
  449. struct drm_prime_member *member;
  450. list_for_each_entry(member, &prime_fpriv->head, entry) {
  451. if (member->dma_buf == dma_buf) {
  452. *handle = member->handle;
  453. return 0;
  454. }
  455. }
  456. return -ENOENT;
  457. }
  458. EXPORT_SYMBOL(drm_prime_lookup_buf_handle);
  459. void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf)
  460. {
  461. struct drm_prime_member *member, *safe;
  462. mutex_lock(&prime_fpriv->lock);
  463. list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
  464. if (member->dma_buf == dma_buf) {
  465. dma_buf_put(dma_buf);
  466. list_del(&member->entry);
  467. kfree(member);
  468. }
  469. }
  470. mutex_unlock(&prime_fpriv->lock);
  471. }
  472. EXPORT_SYMBOL(drm_prime_remove_buf_handle);