Loading...
1/*
2 * Copyright (c) 2015 MediaTek Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <drm/drmP.h>
15#include <drm/drm_gem.h>
16#include <linux/dma-buf.h>
17
18#include "mtk_drm_drv.h"
19#include "mtk_drm_gem.h"
20
21static struct mtk_drm_gem_obj *mtk_drm_gem_init(struct drm_device *dev,
22 unsigned long size)
23{
24 struct mtk_drm_gem_obj *mtk_gem_obj;
25 int ret;
26
27 size = round_up(size, PAGE_SIZE);
28
29 mtk_gem_obj = kzalloc(sizeof(*mtk_gem_obj), GFP_KERNEL);
30 if (!mtk_gem_obj)
31 return ERR_PTR(-ENOMEM);
32
33 ret = drm_gem_object_init(dev, &mtk_gem_obj->base, size);
34 if (ret < 0) {
35 DRM_ERROR("failed to initialize gem object\n");
36 kfree(mtk_gem_obj);
37 return ERR_PTR(ret);
38 }
39
40 return mtk_gem_obj;
41}
42
43struct mtk_drm_gem_obj *mtk_drm_gem_create(struct drm_device *dev,
44 size_t size, bool alloc_kmap)
45{
46 struct mtk_drm_private *priv = dev->dev_private;
47 struct mtk_drm_gem_obj *mtk_gem;
48 struct drm_gem_object *obj;
49 int ret;
50
51 mtk_gem = mtk_drm_gem_init(dev, size);
52 if (IS_ERR(mtk_gem))
53 return ERR_CAST(mtk_gem);
54
55 obj = &mtk_gem->base;
56
57 mtk_gem->dma_attrs = DMA_ATTR_WRITE_COMBINE;
58
59 if (!alloc_kmap)
60 mtk_gem->dma_attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
61
62 mtk_gem->cookie = dma_alloc_attrs(priv->dma_dev, obj->size,
63 &mtk_gem->dma_addr, GFP_KERNEL,
64 mtk_gem->dma_attrs);
65 if (!mtk_gem->cookie) {
66 DRM_ERROR("failed to allocate %zx byte dma buffer", obj->size);
67 ret = -ENOMEM;
68 goto err_gem_free;
69 }
70
71 if (alloc_kmap)
72 mtk_gem->kvaddr = mtk_gem->cookie;
73
74 DRM_DEBUG_DRIVER("cookie = %p dma_addr = %pad size = %zu\n",
75 mtk_gem->cookie, &mtk_gem->dma_addr,
76 size);
77
78 return mtk_gem;
79
80err_gem_free:
81 drm_gem_object_release(obj);
82 kfree(mtk_gem);
83 return ERR_PTR(ret);
84}
85
86void mtk_drm_gem_free_object(struct drm_gem_object *obj)
87{
88 struct mtk_drm_gem_obj *mtk_gem = to_mtk_gem_obj(obj);
89 struct mtk_drm_private *priv = obj->dev->dev_private;
90
91 if (mtk_gem->sg)
92 drm_prime_gem_destroy(obj, mtk_gem->sg);
93 else
94 dma_free_attrs(priv->dma_dev, obj->size, mtk_gem->cookie,
95 mtk_gem->dma_addr, mtk_gem->dma_attrs);
96
97 /* release file pointer to gem object. */
98 drm_gem_object_release(obj);
99
100 kfree(mtk_gem);
101}
102
103int mtk_drm_gem_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
104 struct drm_mode_create_dumb *args)
105{
106 struct mtk_drm_gem_obj *mtk_gem;
107 int ret;
108
109 args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
110 args->size = args->pitch * args->height;
111
112 mtk_gem = mtk_drm_gem_create(dev, args->size, false);
113 if (IS_ERR(mtk_gem))
114 return PTR_ERR(mtk_gem);
115
116 /*
117 * allocate a id of idr table where the obj is registered
118 * and handle has the id what user can see.
119 */
120 ret = drm_gem_handle_create(file_priv, &mtk_gem->base, &args->handle);
121 if (ret)
122 goto err_handle_create;
123
124 /* drop reference from allocate - handle holds it now. */
125 drm_gem_object_put_unlocked(&mtk_gem->base);
126
127 return 0;
128
129err_handle_create:
130 mtk_drm_gem_free_object(&mtk_gem->base);
131 return ret;
132}
133
134static int mtk_drm_gem_object_mmap(struct drm_gem_object *obj,
135 struct vm_area_struct *vma)
136
137{
138 int ret;
139 struct mtk_drm_gem_obj *mtk_gem = to_mtk_gem_obj(obj);
140 struct mtk_drm_private *priv = obj->dev->dev_private;
141
142 /*
143 * dma_alloc_attrs() allocated a struct page table for mtk_gem, so clear
144 * VM_PFNMAP flag that was set by drm_gem_mmap_obj()/drm_gem_mmap().
145 */
146 vma->vm_flags &= ~VM_PFNMAP;
147 vma->vm_pgoff = 0;
148
149 ret = dma_mmap_attrs(priv->dma_dev, vma, mtk_gem->cookie,
150 mtk_gem->dma_addr, obj->size, mtk_gem->dma_attrs);
151 if (ret)
152 drm_gem_vm_close(vma);
153
154 return ret;
155}
156
157int mtk_drm_gem_mmap_buf(struct drm_gem_object *obj, struct vm_area_struct *vma)
158{
159 int ret;
160
161 ret = drm_gem_mmap_obj(obj, obj->size, vma);
162 if (ret)
163 return ret;
164
165 return mtk_drm_gem_object_mmap(obj, vma);
166}
167
168int mtk_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
169{
170 struct drm_gem_object *obj;
171 int ret;
172
173 ret = drm_gem_mmap(filp, vma);
174 if (ret)
175 return ret;
176
177 obj = vma->vm_private_data;
178
179 return mtk_drm_gem_object_mmap(obj, vma);
180}
181
182/*
183 * Allocate a sg_table for this GEM object.
184 * Note: Both the table's contents, and the sg_table itself must be freed by
185 * the caller.
186 * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error.
187 */
188struct sg_table *mtk_gem_prime_get_sg_table(struct drm_gem_object *obj)
189{
190 struct mtk_drm_gem_obj *mtk_gem = to_mtk_gem_obj(obj);
191 struct mtk_drm_private *priv = obj->dev->dev_private;
192 struct sg_table *sgt;
193 int ret;
194
195 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
196 if (!sgt)
197 return ERR_PTR(-ENOMEM);
198
199 ret = dma_get_sgtable_attrs(priv->dma_dev, sgt, mtk_gem->cookie,
200 mtk_gem->dma_addr, obj->size,
201 mtk_gem->dma_attrs);
202 if (ret) {
203 DRM_ERROR("failed to allocate sgt, %d\n", ret);
204 kfree(sgt);
205 return ERR_PTR(ret);
206 }
207
208 return sgt;
209}
210
211struct drm_gem_object *mtk_gem_prime_import_sg_table(struct drm_device *dev,
212 struct dma_buf_attachment *attach, struct sg_table *sg)
213{
214 struct mtk_drm_gem_obj *mtk_gem;
215 int ret;
216 struct scatterlist *s;
217 unsigned int i;
218 dma_addr_t expected;
219
220 mtk_gem = mtk_drm_gem_init(dev, attach->dmabuf->size);
221
222 if (IS_ERR(mtk_gem))
223 return ERR_PTR(PTR_ERR(mtk_gem));
224
225 expected = sg_dma_address(sg->sgl);
226 for_each_sg(sg->sgl, s, sg->nents, i) {
227 if (sg_dma_address(s) != expected) {
228 DRM_ERROR("sg_table is not contiguous");
229 ret = -EINVAL;
230 goto err_gem_free;
231 }
232 expected = sg_dma_address(s) + sg_dma_len(s);
233 }
234
235 mtk_gem->dma_addr = sg_dma_address(sg->sgl);
236 mtk_gem->sg = sg;
237
238 return &mtk_gem->base;
239
240err_gem_free:
241 kfree(mtk_gem);
242 return ERR_PTR(ret);
243}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (c) 2015 MediaTek Inc.
4 */
5
6#include <linux/dma-buf.h>
7
8#include <drm/drm.h>
9#include <drm/drm_device.h>
10#include <drm/drm_gem.h>
11#include <drm/drm_gem_dma_helper.h>
12#include <drm/drm_prime.h>
13
14#include "mtk_drm_drv.h"
15#include "mtk_drm_gem.h"
16
17static int mtk_drm_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
18
19static const struct drm_gem_object_funcs mtk_drm_gem_object_funcs = {
20 .free = mtk_drm_gem_free_object,
21 .get_sg_table = mtk_gem_prime_get_sg_table,
22 .vmap = mtk_drm_gem_prime_vmap,
23 .vunmap = mtk_drm_gem_prime_vunmap,
24 .mmap = mtk_drm_gem_object_mmap,
25 .vm_ops = &drm_gem_dma_vm_ops,
26};
27
28static struct mtk_drm_gem_obj *mtk_drm_gem_init(struct drm_device *dev,
29 unsigned long size)
30{
31 struct mtk_drm_gem_obj *mtk_gem_obj;
32 int ret;
33
34 size = round_up(size, PAGE_SIZE);
35
36 mtk_gem_obj = kzalloc(sizeof(*mtk_gem_obj), GFP_KERNEL);
37 if (!mtk_gem_obj)
38 return ERR_PTR(-ENOMEM);
39
40 mtk_gem_obj->base.funcs = &mtk_drm_gem_object_funcs;
41
42 ret = drm_gem_object_init(dev, &mtk_gem_obj->base, size);
43 if (ret < 0) {
44 DRM_ERROR("failed to initialize gem object\n");
45 kfree(mtk_gem_obj);
46 return ERR_PTR(ret);
47 }
48
49 return mtk_gem_obj;
50}
51
52struct mtk_drm_gem_obj *mtk_drm_gem_create(struct drm_device *dev,
53 size_t size, bool alloc_kmap)
54{
55 struct mtk_drm_private *priv = dev->dev_private;
56 struct mtk_drm_gem_obj *mtk_gem;
57 struct drm_gem_object *obj;
58 int ret;
59
60 mtk_gem = mtk_drm_gem_init(dev, size);
61 if (IS_ERR(mtk_gem))
62 return ERR_CAST(mtk_gem);
63
64 obj = &mtk_gem->base;
65
66 mtk_gem->dma_attrs = DMA_ATTR_WRITE_COMBINE;
67
68 if (!alloc_kmap)
69 mtk_gem->dma_attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
70
71 mtk_gem->cookie = dma_alloc_attrs(priv->dma_dev, obj->size,
72 &mtk_gem->dma_addr, GFP_KERNEL,
73 mtk_gem->dma_attrs);
74 if (!mtk_gem->cookie) {
75 DRM_ERROR("failed to allocate %zx byte dma buffer", obj->size);
76 ret = -ENOMEM;
77 goto err_gem_free;
78 }
79
80 if (alloc_kmap)
81 mtk_gem->kvaddr = mtk_gem->cookie;
82
83 DRM_DEBUG_DRIVER("cookie = %p dma_addr = %pad size = %zu\n",
84 mtk_gem->cookie, &mtk_gem->dma_addr,
85 size);
86
87 return mtk_gem;
88
89err_gem_free:
90 drm_gem_object_release(obj);
91 kfree(mtk_gem);
92 return ERR_PTR(ret);
93}
94
95void mtk_drm_gem_free_object(struct drm_gem_object *obj)
96{
97 struct mtk_drm_gem_obj *mtk_gem = to_mtk_gem_obj(obj);
98 struct mtk_drm_private *priv = obj->dev->dev_private;
99
100 if (mtk_gem->sg)
101 drm_prime_gem_destroy(obj, mtk_gem->sg);
102 else
103 dma_free_attrs(priv->dma_dev, obj->size, mtk_gem->cookie,
104 mtk_gem->dma_addr, mtk_gem->dma_attrs);
105
106 /* release file pointer to gem object. */
107 drm_gem_object_release(obj);
108
109 kfree(mtk_gem);
110}
111
112int mtk_drm_gem_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
113 struct drm_mode_create_dumb *args)
114{
115 struct mtk_drm_gem_obj *mtk_gem;
116 int ret;
117
118 args->pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
119 args->size = args->pitch * args->height;
120
121 mtk_gem = mtk_drm_gem_create(dev, args->size, false);
122 if (IS_ERR(mtk_gem))
123 return PTR_ERR(mtk_gem);
124
125 /*
126 * allocate a id of idr table where the obj is registered
127 * and handle has the id what user can see.
128 */
129 ret = drm_gem_handle_create(file_priv, &mtk_gem->base, &args->handle);
130 if (ret)
131 goto err_handle_create;
132
133 /* drop reference from allocate - handle holds it now. */
134 drm_gem_object_put(&mtk_gem->base);
135
136 return 0;
137
138err_handle_create:
139 mtk_drm_gem_free_object(&mtk_gem->base);
140 return ret;
141}
142
143static int mtk_drm_gem_object_mmap(struct drm_gem_object *obj,
144 struct vm_area_struct *vma)
145
146{
147 int ret;
148 struct mtk_drm_gem_obj *mtk_gem = to_mtk_gem_obj(obj);
149 struct mtk_drm_private *priv = obj->dev->dev_private;
150
151 /*
152 * Set vm_pgoff (used as a fake buffer offset by DRM) to 0 and map the
153 * whole buffer from the start.
154 */
155 vma->vm_pgoff = 0;
156
157 /*
158 * dma_alloc_attrs() allocated a struct page table for mtk_gem, so clear
159 * VM_PFNMAP flag that was set by drm_gem_mmap_obj()/drm_gem_mmap().
160 */
161 vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
162 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
163 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
164
165 ret = dma_mmap_attrs(priv->dma_dev, vma, mtk_gem->cookie,
166 mtk_gem->dma_addr, obj->size, mtk_gem->dma_attrs);
167 if (ret)
168 drm_gem_vm_close(vma);
169
170 return ret;
171}
172
173/*
174 * Allocate a sg_table for this GEM object.
175 * Note: Both the table's contents, and the sg_table itself must be freed by
176 * the caller.
177 * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error.
178 */
179struct sg_table *mtk_gem_prime_get_sg_table(struct drm_gem_object *obj)
180{
181 struct mtk_drm_gem_obj *mtk_gem = to_mtk_gem_obj(obj);
182 struct mtk_drm_private *priv = obj->dev->dev_private;
183 struct sg_table *sgt;
184 int ret;
185
186 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
187 if (!sgt)
188 return ERR_PTR(-ENOMEM);
189
190 ret = dma_get_sgtable_attrs(priv->dma_dev, sgt, mtk_gem->cookie,
191 mtk_gem->dma_addr, obj->size,
192 mtk_gem->dma_attrs);
193 if (ret) {
194 DRM_ERROR("failed to allocate sgt, %d\n", ret);
195 kfree(sgt);
196 return ERR_PTR(ret);
197 }
198
199 return sgt;
200}
201
202struct drm_gem_object *mtk_gem_prime_import_sg_table(struct drm_device *dev,
203 struct dma_buf_attachment *attach, struct sg_table *sg)
204{
205 struct mtk_drm_gem_obj *mtk_gem;
206
207 /* check if the entries in the sg_table are contiguous */
208 if (drm_prime_get_contiguous_size(sg) < attach->dmabuf->size) {
209 DRM_ERROR("sg_table is not contiguous");
210 return ERR_PTR(-EINVAL);
211 }
212
213 mtk_gem = mtk_drm_gem_init(dev, attach->dmabuf->size);
214 if (IS_ERR(mtk_gem))
215 return ERR_CAST(mtk_gem);
216
217 mtk_gem->dma_addr = sg_dma_address(sg->sgl);
218 mtk_gem->sg = sg;
219
220 return &mtk_gem->base;
221}
222
223int mtk_drm_gem_prime_vmap(struct drm_gem_object *obj, struct iosys_map *map)
224{
225 struct mtk_drm_gem_obj *mtk_gem = to_mtk_gem_obj(obj);
226 struct sg_table *sgt = NULL;
227 unsigned int npages;
228
229 if (mtk_gem->kvaddr)
230 goto out;
231
232 sgt = mtk_gem_prime_get_sg_table(obj);
233 if (IS_ERR(sgt))
234 return PTR_ERR(sgt);
235
236 npages = obj->size >> PAGE_SHIFT;
237 mtk_gem->pages = kcalloc(npages, sizeof(*mtk_gem->pages), GFP_KERNEL);
238 if (!mtk_gem->pages) {
239 kfree(sgt);
240 return -ENOMEM;
241 }
242
243 drm_prime_sg_to_page_array(sgt, mtk_gem->pages, npages);
244
245 mtk_gem->kvaddr = vmap(mtk_gem->pages, npages, VM_MAP,
246 pgprot_writecombine(PAGE_KERNEL));
247
248out:
249 kfree(sgt);
250 iosys_map_set_vaddr(map, mtk_gem->kvaddr);
251
252 return 0;
253}
254
255void mtk_drm_gem_prime_vunmap(struct drm_gem_object *obj,
256 struct iosys_map *map)
257{
258 struct mtk_drm_gem_obj *mtk_gem = to_mtk_gem_obj(obj);
259 void *vaddr = map->vaddr;
260
261 if (!mtk_gem->pages)
262 return;
263
264 vunmap(vaddr);
265 mtk_gem->kvaddr = 0;
266 kfree(mtk_gem->pages);
267}