Loading...
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 */
6
7#include <linux/dma-buf.h>
8#include <linux/highmem.h>
9
10#include <drm/drm_prime.h>
11
12#include "omap_drv.h"
13
14MODULE_IMPORT_NS("DMA_BUF");
15
16/* -----------------------------------------------------------------------------
17 * DMABUF Export
18 */
19
20static struct sg_table *omap_gem_map_dma_buf(
21 struct dma_buf_attachment *attachment,
22 enum dma_data_direction dir)
23{
24 struct drm_gem_object *obj = attachment->dmabuf->priv;
25 struct sg_table *sg;
26 sg = omap_gem_get_sg(obj, dir);
27 if (IS_ERR(sg))
28 return sg;
29
30 return sg;
31}
32
33static void omap_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
34 struct sg_table *sg, enum dma_data_direction dir)
35{
36 struct drm_gem_object *obj = attachment->dmabuf->priv;
37 omap_gem_put_sg(obj, sg);
38}
39
40static int omap_gem_dmabuf_begin_cpu_access(struct dma_buf *buffer,
41 enum dma_data_direction dir)
42{
43 struct drm_gem_object *obj = buffer->priv;
44 struct page **pages;
45 if (omap_gem_flags(obj) & OMAP_BO_TILED_MASK) {
46 /* TODO we would need to pin at least part of the buffer to
47 * get de-tiled view. For now just reject it.
48 */
49 return -ENOMEM;
50 }
51 /* make sure we have the pages: */
52 return omap_gem_get_pages(obj, &pages, true);
53}
54
55static int omap_gem_dmabuf_end_cpu_access(struct dma_buf *buffer,
56 enum dma_data_direction dir)
57{
58 struct drm_gem_object *obj = buffer->priv;
59 omap_gem_put_pages(obj);
60 return 0;
61}
62
63static int omap_gem_dmabuf_mmap(struct dma_buf *buffer,
64 struct vm_area_struct *vma)
65{
66 struct drm_gem_object *obj = buffer->priv;
67
68 return drm_gem_mmap_obj(obj, omap_gem_mmap_size(obj), vma);
69}
70
71static const struct dma_buf_ops omap_dmabuf_ops = {
72 .map_dma_buf = omap_gem_map_dma_buf,
73 .unmap_dma_buf = omap_gem_unmap_dma_buf,
74 .release = drm_gem_dmabuf_release,
75 .begin_cpu_access = omap_gem_dmabuf_begin_cpu_access,
76 .end_cpu_access = omap_gem_dmabuf_end_cpu_access,
77 .mmap = omap_gem_dmabuf_mmap,
78};
79
80struct dma_buf *omap_gem_prime_export(struct drm_gem_object *obj, int flags)
81{
82 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
83
84 exp_info.ops = &omap_dmabuf_ops;
85 exp_info.size = omap_gem_mmap_size(obj);
86 exp_info.flags = flags;
87 exp_info.priv = obj;
88 exp_info.resv = obj->resv;
89
90 return drm_gem_dmabuf_export(obj->dev, &exp_info);
91}
92
93/* -----------------------------------------------------------------------------
94 * DMABUF Import
95 */
96
97struct drm_gem_object *omap_gem_prime_import(struct drm_device *dev,
98 struct dma_buf *dma_buf)
99{
100 struct dma_buf_attachment *attach;
101 struct drm_gem_object *obj;
102 struct sg_table *sgt;
103 int ret;
104
105 if (dma_buf->ops == &omap_dmabuf_ops) {
106 obj = dma_buf->priv;
107 if (obj->dev == dev) {
108 /*
109 * Importing dmabuf exported from out own gem increases
110 * refcount on gem itself instead of f_count of dmabuf.
111 */
112 drm_gem_object_get(obj);
113 return obj;
114 }
115 }
116
117 attach = dma_buf_attach(dma_buf, dev->dev);
118 if (IS_ERR(attach))
119 return ERR_CAST(attach);
120
121 get_dma_buf(dma_buf);
122
123 sgt = dma_buf_map_attachment_unlocked(attach, DMA_TO_DEVICE);
124 if (IS_ERR(sgt)) {
125 ret = PTR_ERR(sgt);
126 goto fail_detach;
127 }
128
129 obj = omap_gem_new_dmabuf(dev, dma_buf->size, sgt);
130 if (IS_ERR(obj)) {
131 ret = PTR_ERR(obj);
132 goto fail_unmap;
133 }
134
135 obj->import_attach = attach;
136
137 return obj;
138
139fail_unmap:
140 dma_buf_unmap_attachment_unlocked(attach, sgt, DMA_TO_DEVICE);
141fail_detach:
142 dma_buf_detach(dma_buf, attach);
143 dma_buf_put(dma_buf);
144
145 return ERR_PTR(ret);
146}
1/*
2 * drivers/gpu/drm/omapdrm/omap_gem_dmabuf.c
3 *
4 * Copyright (C) 2011 Texas Instruments
5 * Author: Rob Clark <rob.clark@linaro.org>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "omap_drv.h"
21
22#include <linux/dma-buf.h>
23
24static struct sg_table *omap_gem_map_dma_buf(
25 struct dma_buf_attachment *attachment,
26 enum dma_data_direction dir)
27{
28 struct drm_gem_object *obj = attachment->dmabuf->priv;
29 struct sg_table *sg;
30 dma_addr_t paddr;
31 int ret;
32
33 sg = kzalloc(sizeof(*sg), GFP_KERNEL);
34 if (!sg)
35 return ERR_PTR(-ENOMEM);
36
37 /* camera, etc, need physically contiguous.. but we need a
38 * better way to know this..
39 */
40 ret = omap_gem_get_paddr(obj, &paddr, true);
41 if (ret)
42 goto out;
43
44 ret = sg_alloc_table(sg, 1, GFP_KERNEL);
45 if (ret)
46 goto out;
47
48 sg_init_table(sg->sgl, 1);
49 sg_dma_len(sg->sgl) = obj->size;
50 sg_set_page(sg->sgl, pfn_to_page(PFN_DOWN(paddr)), obj->size, 0);
51 sg_dma_address(sg->sgl) = paddr;
52
53 /* this should be after _get_paddr() to ensure we have pages attached */
54 omap_gem_dma_sync(obj, dir);
55
56 return sg;
57out:
58 kfree(sg);
59 return ERR_PTR(ret);
60}
61
62static void omap_gem_unmap_dma_buf(struct dma_buf_attachment *attachment,
63 struct sg_table *sg, enum dma_data_direction dir)
64{
65 struct drm_gem_object *obj = attachment->dmabuf->priv;
66 omap_gem_put_paddr(obj);
67 sg_free_table(sg);
68 kfree(sg);
69}
70
71static void omap_gem_dmabuf_release(struct dma_buf *buffer)
72{
73 struct drm_gem_object *obj = buffer->priv;
74 /* release reference that was taken when dmabuf was exported
75 * in omap_gem_prime_set()..
76 */
77 drm_gem_object_unreference_unlocked(obj);
78}
79
80
81static int omap_gem_dmabuf_begin_cpu_access(struct dma_buf *buffer,
82 size_t start, size_t len, enum dma_data_direction dir)
83{
84 struct drm_gem_object *obj = buffer->priv;
85 struct page **pages;
86 if (omap_gem_flags(obj) & OMAP_BO_TILED) {
87 /* TODO we would need to pin at least part of the buffer to
88 * get de-tiled view. For now just reject it.
89 */
90 return -ENOMEM;
91 }
92 /* make sure we have the pages: */
93 return omap_gem_get_pages(obj, &pages, true);
94}
95
96static void omap_gem_dmabuf_end_cpu_access(struct dma_buf *buffer,
97 size_t start, size_t len, enum dma_data_direction dir)
98{
99 struct drm_gem_object *obj = buffer->priv;
100 omap_gem_put_pages(obj);
101}
102
103
104static void *omap_gem_dmabuf_kmap_atomic(struct dma_buf *buffer,
105 unsigned long page_num)
106{
107 struct drm_gem_object *obj = buffer->priv;
108 struct page **pages;
109 omap_gem_get_pages(obj, &pages, false);
110 omap_gem_cpu_sync(obj, page_num);
111 return kmap_atomic(pages[page_num]);
112}
113
114static void omap_gem_dmabuf_kunmap_atomic(struct dma_buf *buffer,
115 unsigned long page_num, void *addr)
116{
117 kunmap_atomic(addr);
118}
119
120static void *omap_gem_dmabuf_kmap(struct dma_buf *buffer,
121 unsigned long page_num)
122{
123 struct drm_gem_object *obj = buffer->priv;
124 struct page **pages;
125 omap_gem_get_pages(obj, &pages, false);
126 omap_gem_cpu_sync(obj, page_num);
127 return kmap(pages[page_num]);
128}
129
130static void omap_gem_dmabuf_kunmap(struct dma_buf *buffer,
131 unsigned long page_num, void *addr)
132{
133 struct drm_gem_object *obj = buffer->priv;
134 struct page **pages;
135 omap_gem_get_pages(obj, &pages, false);
136 kunmap(pages[page_num]);
137}
138
139static int omap_gem_dmabuf_mmap(struct dma_buf *buffer,
140 struct vm_area_struct *vma)
141{
142 struct drm_gem_object *obj = buffer->priv;
143 struct drm_device *dev = obj->dev;
144 int ret = 0;
145
146 if (WARN_ON(!obj->filp))
147 return -EINVAL;
148
149 mutex_lock(&dev->struct_mutex);
150 ret = drm_gem_mmap_obj(obj, omap_gem_mmap_size(obj), vma);
151 mutex_unlock(&dev->struct_mutex);
152 if (ret < 0)
153 return ret;
154
155 return omap_gem_mmap_obj(obj, vma);
156}
157
158static struct dma_buf_ops omap_dmabuf_ops = {
159 .map_dma_buf = omap_gem_map_dma_buf,
160 .unmap_dma_buf = omap_gem_unmap_dma_buf,
161 .release = omap_gem_dmabuf_release,
162 .begin_cpu_access = omap_gem_dmabuf_begin_cpu_access,
163 .end_cpu_access = omap_gem_dmabuf_end_cpu_access,
164 .kmap_atomic = omap_gem_dmabuf_kmap_atomic,
165 .kunmap_atomic = omap_gem_dmabuf_kunmap_atomic,
166 .kmap = omap_gem_dmabuf_kmap,
167 .kunmap = omap_gem_dmabuf_kunmap,
168 .mmap = omap_gem_dmabuf_mmap,
169};
170
171struct dma_buf *omap_gem_prime_export(struct drm_device *dev,
172 struct drm_gem_object *obj, int flags)
173{
174 return dma_buf_export(obj, &omap_dmabuf_ops, obj->size, flags);
175}
176
177struct drm_gem_object *omap_gem_prime_import(struct drm_device *dev,
178 struct dma_buf *buffer)
179{
180 struct drm_gem_object *obj;
181
182 /* is this one of own objects? */
183 if (buffer->ops == &omap_dmabuf_ops) {
184 obj = buffer->priv;
185 /* is it from our device? */
186 if (obj->dev == dev) {
187 /*
188 * Importing dmabuf exported from out own gem increases
189 * refcount on gem itself instead of f_count of dmabuf.
190 */
191 drm_gem_object_reference(obj);
192 return obj;
193 }
194 }
195
196 /*
197 * TODO add support for importing buffers from other devices..
198 * for now we don't need this but would be nice to add eventually
199 */
200 return ERR_PTR(-EINVAL);
201}