drm_prime.c 16 KB

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