Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / omapdrm / omap_gem_dmabuf.c
blobf4cde3a169d8dd6fbae9a2c947db0c3c43eea247
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
4 * Author: Rob Clark <rob.clark@linaro.org>
5 */
7 #include <linux/dma-buf.h>
8 #include <linux/highmem.h>
10 #include <drm/drm_prime.h>
12 #include "omap_drv.h"
14 /* -----------------------------------------------------------------------------
15 * DMABUF Export
18 static struct sg_table *omap_gem_map_dma_buf(
19 struct dma_buf_attachment *attachment,
20 enum dma_data_direction dir)
22 struct drm_gem_object *obj = attachment->dmabuf->priv;
23 struct sg_table *sg;
24 dma_addr_t dma_addr;
25 int ret;
27 sg = kzalloc(sizeof(*sg), GFP_KERNEL);
28 if (!sg)
29 return ERR_PTR(-ENOMEM);
31 /* camera, etc, need physically contiguous.. but we need a
32 * better way to know this..
34 ret = omap_gem_pin(obj, &dma_addr);
35 if (ret)
36 goto out;
38 ret = sg_alloc_table(sg, 1, GFP_KERNEL);
39 if (ret)
40 goto out;
42 sg_init_table(sg->sgl, 1);
43 sg_dma_len(sg->sgl) = obj->size;
44 sg_set_page(sg->sgl, pfn_to_page(PFN_DOWN(dma_addr)), obj->size, 0);
45 sg_dma_address(sg->sgl) = dma_addr;
47 /* this must be after omap_gem_pin() to ensure we have pages attached */
48 omap_gem_dma_sync_buffer(obj, dir);
50 return sg;
51 out:
52 kfree(sg);
53 return ERR_PTR(ret);
56 static void omap_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
57 struct sg_table *sg, enum dma_data_direction dir)
59 struct drm_gem_object *obj = attachment->dmabuf->priv;
60 omap_gem_unpin(obj);
61 sg_free_table(sg);
62 kfree(sg);
65 static int omap_gem_dmabuf_begin_cpu_access(struct dma_buf *buffer,
66 enum dma_data_direction dir)
68 struct drm_gem_object *obj = buffer->priv;
69 struct page **pages;
70 if (omap_gem_flags(obj) & OMAP_BO_TILED_MASK) {
71 /* TODO we would need to pin at least part of the buffer to
72 * get de-tiled view. For now just reject it.
74 return -ENOMEM;
76 /* make sure we have the pages: */
77 return omap_gem_get_pages(obj, &pages, true);
80 static int omap_gem_dmabuf_end_cpu_access(struct dma_buf *buffer,
81 enum dma_data_direction dir)
83 struct drm_gem_object *obj = buffer->priv;
84 omap_gem_put_pages(obj);
85 return 0;
88 static int omap_gem_dmabuf_mmap(struct dma_buf *buffer,
89 struct vm_area_struct *vma)
91 struct drm_gem_object *obj = buffer->priv;
92 int ret = 0;
94 ret = drm_gem_mmap_obj(obj, omap_gem_mmap_size(obj), vma);
95 if (ret < 0)
96 return ret;
98 return omap_gem_mmap_obj(obj, vma);
101 static const struct dma_buf_ops omap_dmabuf_ops = {
102 .map_dma_buf = omap_gem_map_dma_buf,
103 .unmap_dma_buf = omap_gem_unmap_dma_buf,
104 .release = drm_gem_dmabuf_release,
105 .begin_cpu_access = omap_gem_dmabuf_begin_cpu_access,
106 .end_cpu_access = omap_gem_dmabuf_end_cpu_access,
107 .mmap = omap_gem_dmabuf_mmap,
110 struct dma_buf *omap_gem_prime_export(struct drm_gem_object *obj, int flags)
112 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
114 exp_info.ops = &omap_dmabuf_ops;
115 exp_info.size = obj->size;
116 exp_info.flags = flags;
117 exp_info.priv = obj;
119 return drm_gem_dmabuf_export(obj->dev, &exp_info);
122 /* -----------------------------------------------------------------------------
123 * DMABUF Import
126 struct drm_gem_object *omap_gem_prime_import(struct drm_device *dev,
127 struct dma_buf *dma_buf)
129 struct dma_buf_attachment *attach;
130 struct drm_gem_object *obj;
131 struct sg_table *sgt;
132 int ret;
134 if (dma_buf->ops == &omap_dmabuf_ops) {
135 obj = dma_buf->priv;
136 if (obj->dev == dev) {
138 * Importing dmabuf exported from out own gem increases
139 * refcount on gem itself instead of f_count of dmabuf.
141 drm_gem_object_get(obj);
142 return obj;
146 attach = dma_buf_attach(dma_buf, dev->dev);
147 if (IS_ERR(attach))
148 return ERR_CAST(attach);
150 get_dma_buf(dma_buf);
152 sgt = dma_buf_map_attachment(attach, DMA_TO_DEVICE);
153 if (IS_ERR(sgt)) {
154 ret = PTR_ERR(sgt);
155 goto fail_detach;
158 obj = omap_gem_new_dmabuf(dev, dma_buf->size, sgt);
159 if (IS_ERR(obj)) {
160 ret = PTR_ERR(obj);
161 goto fail_unmap;
164 obj->import_attach = attach;
166 return obj;
168 fail_unmap:
169 dma_buf_unmap_attachment(attach, sgt, DMA_TO_DEVICE);
170 fail_detach:
171 dma_buf_detach(dma_buf, attach);
172 dma_buf_put(dma_buf);
174 return ERR_PTR(ret);