drm_prime.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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(sgt)) {
  89. if (!dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir)) {
  90. sg_free_table(sgt);
  91. kfree(sgt);
  92. sgt = ERR_PTR(-ENOMEM);
  93. }
  94. }
  95. mutex_unlock(&obj->dev->struct_mutex);
  96. return sgt;
  97. }
  98. static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
  99. struct sg_table *sgt, enum dma_data_direction dir)
  100. {
  101. dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
  102. sg_free_table(sgt);
  103. kfree(sgt);
  104. }
  105. static void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
  106. {
  107. struct drm_gem_object *obj = dma_buf->priv;
  108. if (obj->export_dma_buf == dma_buf) {
  109. /* drop the reference on the export fd holds */
  110. obj->export_dma_buf = NULL;
  111. drm_gem_object_unreference_unlocked(obj);
  112. }
  113. }
  114. static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
  115. {
  116. struct drm_gem_object *obj = dma_buf->priv;
  117. struct drm_device *dev = obj->dev;
  118. return dev->driver->gem_prime_vmap(obj);
  119. }
  120. static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
  121. {
  122. struct drm_gem_object *obj = dma_buf->priv;
  123. struct drm_device *dev = obj->dev;
  124. dev->driver->gem_prime_vunmap(obj, vaddr);
  125. }
  126. static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
  127. unsigned long page_num)
  128. {
  129. return NULL;
  130. }
  131. static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
  132. unsigned long page_num, void *addr)
  133. {
  134. }
  135. static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
  136. unsigned long page_num)
  137. {
  138. return NULL;
  139. }
  140. static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
  141. unsigned long page_num, void *addr)
  142. {
  143. }
  144. static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
  145. struct vm_area_struct *vma)
  146. {
  147. return -EINVAL;
  148. }
  149. static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
  150. .attach = drm_gem_map_attach,
  151. .detach = drm_gem_map_detach,
  152. .map_dma_buf = drm_gem_map_dma_buf,
  153. .unmap_dma_buf = drm_gem_unmap_dma_buf,
  154. .release = drm_gem_dmabuf_release,
  155. .kmap = drm_gem_dmabuf_kmap,
  156. .kmap_atomic = drm_gem_dmabuf_kmap_atomic,
  157. .kunmap = drm_gem_dmabuf_kunmap,
  158. .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
  159. .mmap = drm_gem_dmabuf_mmap,
  160. .vmap = drm_gem_dmabuf_vmap,
  161. .vunmap = drm_gem_dmabuf_vunmap,
  162. };
  163. /**
  164. * DOC: PRIME Helpers
  165. *
  166. * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
  167. * simpler APIs by using the helper functions @drm_gem_prime_export and
  168. * @drm_gem_prime_import. These functions implement dma-buf support in terms of
  169. * five lower-level driver callbacks:
  170. *
  171. * Export callbacks:
  172. *
  173. * - @gem_prime_pin (optional): prepare a GEM object for exporting
  174. *
  175. * - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
  176. *
  177. * - @gem_prime_vmap: vmap a buffer exported by your driver
  178. *
  179. * - @gem_prime_vunmap: vunmap a buffer exported by your driver
  180. *
  181. * Import callback:
  182. *
  183. * - @gem_prime_import_sg_table (import): produce a GEM object from another
  184. * driver's scatter/gather table
  185. */
  186. struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
  187. struct drm_gem_object *obj, int flags)
  188. {
  189. return dma_buf_export(obj, &drm_gem_prime_dmabuf_ops, obj->size, flags);
  190. }
  191. EXPORT_SYMBOL(drm_gem_prime_export);
  192. int drm_gem_prime_handle_to_fd(struct drm_device *dev,
  193. struct drm_file *file_priv, uint32_t handle, uint32_t flags,
  194. int *prime_fd)
  195. {
  196. struct drm_gem_object *obj;
  197. void *buf;
  198. int ret = 0;
  199. struct dma_buf *dmabuf;
  200. obj = drm_gem_object_lookup(dev, file_priv, handle);
  201. if (!obj)
  202. return -ENOENT;
  203. mutex_lock(&file_priv->prime.lock);
  204. /* re-export the original imported object */
  205. if (obj->import_attach) {
  206. dmabuf = obj->import_attach->dmabuf;
  207. goto out_have_obj;
  208. }
  209. if (obj->export_dma_buf) {
  210. dmabuf = obj->export_dma_buf;
  211. goto out_have_obj;
  212. }
  213. buf = dev->driver->gem_prime_export(dev, obj, flags);
  214. if (IS_ERR(buf)) {
  215. /* normally the created dma-buf takes ownership of the ref,
  216. * but if that fails then drop the ref
  217. */
  218. ret = PTR_ERR(buf);
  219. goto out;
  220. }
  221. obj->export_dma_buf = buf;
  222. /* if we've exported this buffer the cheat and add it to the import list
  223. * so we get the correct handle back
  224. */
  225. ret = drm_prime_add_buf_handle(&file_priv->prime,
  226. obj->export_dma_buf, handle);
  227. if (ret)
  228. goto out;
  229. *prime_fd = dma_buf_fd(buf, flags);
  230. mutex_unlock(&file_priv->prime.lock);
  231. return 0;
  232. out_have_obj:
  233. get_dma_buf(dmabuf);
  234. *prime_fd = dma_buf_fd(dmabuf, flags);
  235. out:
  236. drm_gem_object_unreference_unlocked(obj);
  237. mutex_unlock(&file_priv->prime.lock);
  238. return ret;
  239. }
  240. EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
  241. struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
  242. struct dma_buf *dma_buf)
  243. {
  244. struct dma_buf_attachment *attach;
  245. struct sg_table *sgt;
  246. struct drm_gem_object *obj;
  247. int ret;
  248. if (!dev->driver->gem_prime_import_sg_table)
  249. return ERR_PTR(-EINVAL);
  250. if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
  251. obj = dma_buf->priv;
  252. if (obj->dev == dev) {
  253. /*
  254. * Importing dmabuf exported from out own gem increases
  255. * refcount on gem itself instead of f_count of dmabuf.
  256. */
  257. drm_gem_object_reference(obj);
  258. return obj;
  259. }
  260. }
  261. attach = dma_buf_attach(dma_buf, dev->dev);
  262. if (IS_ERR(attach))
  263. return ERR_CAST(attach);
  264. get_dma_buf(dma_buf);
  265. sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
  266. if (IS_ERR_OR_NULL(sgt)) {
  267. ret = PTR_ERR(sgt);
  268. goto fail_detach;
  269. }
  270. obj = dev->driver->gem_prime_import_sg_table(dev, dma_buf->size, sgt);
  271. if (IS_ERR(obj)) {
  272. ret = PTR_ERR(obj);
  273. goto fail_unmap;
  274. }
  275. obj->import_attach = attach;
  276. return obj;
  277. fail_unmap:
  278. dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
  279. fail_detach:
  280. dma_buf_detach(dma_buf, attach);
  281. dma_buf_put(dma_buf);
  282. return ERR_PTR(ret);
  283. }
  284. EXPORT_SYMBOL(drm_gem_prime_import);
  285. int drm_gem_prime_fd_to_handle(struct drm_device *dev,
  286. struct drm_file *file_priv, int prime_fd, uint32_t *handle)
  287. {
  288. struct dma_buf *dma_buf;
  289. struct drm_gem_object *obj;
  290. int ret;
  291. dma_buf = dma_buf_get(prime_fd);
  292. if (IS_ERR(dma_buf))
  293. return PTR_ERR(dma_buf);
  294. mutex_lock(&file_priv->prime.lock);
  295. ret = drm_prime_lookup_buf_handle(&file_priv->prime,
  296. dma_buf, handle);
  297. if (!ret) {
  298. ret = 0;
  299. goto out_put;
  300. }
  301. /* never seen this one, need to import */
  302. obj = dev->driver->gem_prime_import(dev, dma_buf);
  303. if (IS_ERR(obj)) {
  304. ret = PTR_ERR(obj);
  305. goto out_put;
  306. }
  307. ret = drm_gem_handle_create(file_priv, obj, handle);
  308. drm_gem_object_unreference_unlocked(obj);
  309. if (ret)
  310. goto out_put;
  311. ret = drm_prime_add_buf_handle(&file_priv->prime,
  312. dma_buf, *handle);
  313. if (ret)
  314. goto fail;
  315. mutex_unlock(&file_priv->prime.lock);
  316. dma_buf_put(dma_buf);
  317. return 0;
  318. fail:
  319. /* hmm, if driver attached, we are relying on the free-object path
  320. * to detach.. which seems ok..
  321. */
  322. drm_gem_object_handle_unreference_unlocked(obj);
  323. out_put:
  324. dma_buf_put(dma_buf);
  325. mutex_unlock(&file_priv->prime.lock);
  326. return ret;
  327. }
  328. EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
  329. int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
  330. struct drm_file *file_priv)
  331. {
  332. struct drm_prime_handle *args = data;
  333. uint32_t flags;
  334. if (!drm_core_check_feature(dev, DRIVER_PRIME))
  335. return -EINVAL;
  336. if (!dev->driver->prime_handle_to_fd)
  337. return -ENOSYS;
  338. /* check flags are valid */
  339. if (args->flags & ~DRM_CLOEXEC)
  340. return -EINVAL;
  341. /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
  342. flags = args->flags & DRM_CLOEXEC;
  343. return dev->driver->prime_handle_to_fd(dev, file_priv,
  344. args->handle, flags, &args->fd);
  345. }
  346. int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
  347. struct drm_file *file_priv)
  348. {
  349. struct drm_prime_handle *args = data;
  350. if (!drm_core_check_feature(dev, DRIVER_PRIME))
  351. return -EINVAL;
  352. if (!dev->driver->prime_fd_to_handle)
  353. return -ENOSYS;
  354. return dev->driver->prime_fd_to_handle(dev, file_priv,
  355. args->fd, &args->handle);
  356. }
  357. /*
  358. * drm_prime_pages_to_sg
  359. *
  360. * this helper creates an sg table object from a set of pages
  361. * the driver is responsible for mapping the pages into the
  362. * importers address space
  363. */
  364. struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages)
  365. {
  366. struct sg_table *sg = NULL;
  367. int ret;
  368. sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
  369. if (!sg) {
  370. ret = -ENOMEM;
  371. goto out;
  372. }
  373. ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
  374. nr_pages << PAGE_SHIFT, GFP_KERNEL);
  375. if (ret)
  376. goto out;
  377. return sg;
  378. out:
  379. kfree(sg);
  380. return ERR_PTR(ret);
  381. }
  382. EXPORT_SYMBOL(drm_prime_pages_to_sg);
  383. /* export an sg table into an array of pages and addresses
  384. this is currently required by the TTM driver in order to do correct fault
  385. handling */
  386. int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
  387. dma_addr_t *addrs, int max_pages)
  388. {
  389. unsigned count;
  390. struct scatterlist *sg;
  391. struct page *page;
  392. u32 len, offset;
  393. int pg_index;
  394. dma_addr_t addr;
  395. pg_index = 0;
  396. for_each_sg(sgt->sgl, sg, sgt->nents, count) {
  397. len = sg->length;
  398. offset = sg->offset;
  399. page = sg_page(sg);
  400. addr = sg_dma_address(sg);
  401. while (len > 0) {
  402. if (WARN_ON(pg_index >= max_pages))
  403. return -1;
  404. pages[pg_index] = page;
  405. if (addrs)
  406. addrs[pg_index] = addr;
  407. page++;
  408. addr += PAGE_SIZE;
  409. len -= PAGE_SIZE;
  410. pg_index++;
  411. }
  412. }
  413. return 0;
  414. }
  415. EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
  416. /* helper function to cleanup a GEM/prime object */
  417. void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
  418. {
  419. struct dma_buf_attachment *attach;
  420. struct dma_buf *dma_buf;
  421. attach = obj->import_attach;
  422. if (sg)
  423. dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
  424. dma_buf = attach->dmabuf;
  425. dma_buf_detach(attach->dmabuf, attach);
  426. /* remove the reference */
  427. dma_buf_put(dma_buf);
  428. }
  429. EXPORT_SYMBOL(drm_prime_gem_destroy);
  430. void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
  431. {
  432. INIT_LIST_HEAD(&prime_fpriv->head);
  433. mutex_init(&prime_fpriv->lock);
  434. }
  435. EXPORT_SYMBOL(drm_prime_init_file_private);
  436. void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
  437. {
  438. /* by now drm_gem_release should've made sure the list is empty */
  439. WARN_ON(!list_empty(&prime_fpriv->head));
  440. }
  441. EXPORT_SYMBOL(drm_prime_destroy_file_private);
  442. static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle)
  443. {
  444. struct drm_prime_member *member;
  445. member = kmalloc(sizeof(*member), GFP_KERNEL);
  446. if (!member)
  447. return -ENOMEM;
  448. get_dma_buf(dma_buf);
  449. member->dma_buf = dma_buf;
  450. member->handle = handle;
  451. list_add(&member->entry, &prime_fpriv->head);
  452. return 0;
  453. }
  454. int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle)
  455. {
  456. struct drm_prime_member *member;
  457. list_for_each_entry(member, &prime_fpriv->head, entry) {
  458. if (member->dma_buf == dma_buf) {
  459. *handle = member->handle;
  460. return 0;
  461. }
  462. }
  463. return -ENOENT;
  464. }
  465. EXPORT_SYMBOL(drm_prime_lookup_buf_handle);
  466. void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf)
  467. {
  468. struct drm_prime_member *member, *safe;
  469. mutex_lock(&prime_fpriv->lock);
  470. list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
  471. if (member->dma_buf == dma_buf) {
  472. dma_buf_put(dma_buf);
  473. list_del(&member->entry);
  474. kfree(member);
  475. }
  476. }
  477. mutex_unlock(&prime_fpriv->lock);
  478. }
  479. EXPORT_SYMBOL(drm_prime_remove_buf_handle);