1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * Author: Inki Dae <inki.dae@samsung.com>
9 #include <linux/dma-buf.h>
10 #include <linux/pfn_t.h>
11 #include <linux/shmem_fs.h>
12 #include <linux/module.h>
14 #include <drm/drm_prime.h>
15 #include <drm/drm_vma_manager.h>
16 #include <drm/exynos_drm.h>
18 #include "exynos_drm_drv.h"
19 #include "exynos_drm_gem.h"
21 MODULE_IMPORT_NS("DMA_BUF");
23 static int exynos_drm_gem_mmap(struct drm_gem_object
*obj
, struct vm_area_struct
*vma
);
25 static int exynos_drm_alloc_buf(struct exynos_drm_gem
*exynos_gem
, bool kvmap
)
27 struct drm_device
*dev
= exynos_gem
->base
.dev
;
28 unsigned long attr
= 0;
30 if (exynos_gem
->dma_addr
) {
31 DRM_DEV_DEBUG_KMS(to_dma_dev(dev
), "already allocated.\n");
36 * if EXYNOS_BO_CONTIG, fully physically contiguous memory
37 * region will be allocated else physically contiguous
40 if (!(exynos_gem
->flags
& EXYNOS_BO_NONCONTIG
))
41 attr
|= DMA_ATTR_FORCE_CONTIGUOUS
;
44 * if EXYNOS_BO_WC or EXYNOS_BO_NONCACHABLE, writecombine mapping
45 * else cachable mapping.
47 if (exynos_gem
->flags
& EXYNOS_BO_WC
||
48 !(exynos_gem
->flags
& EXYNOS_BO_CACHABLE
))
49 attr
|= DMA_ATTR_WRITE_COMBINE
;
51 /* FBDev emulation requires kernel mapping */
53 attr
|= DMA_ATTR_NO_KERNEL_MAPPING
;
55 exynos_gem
->dma_attrs
= attr
;
56 exynos_gem
->cookie
= dma_alloc_attrs(to_dma_dev(dev
), exynos_gem
->size
,
57 &exynos_gem
->dma_addr
, GFP_KERNEL
,
58 exynos_gem
->dma_attrs
);
59 if (!exynos_gem
->cookie
) {
60 DRM_DEV_ERROR(to_dma_dev(dev
), "failed to allocate buffer.\n");
65 exynos_gem
->kvaddr
= exynos_gem
->cookie
;
67 DRM_DEV_DEBUG_KMS(to_dma_dev(dev
), "dma_addr(0x%lx), size(0x%lx)\n",
68 (unsigned long)exynos_gem
->dma_addr
, exynos_gem
->size
);
72 static void exynos_drm_free_buf(struct exynos_drm_gem
*exynos_gem
)
74 struct drm_device
*dev
= exynos_gem
->base
.dev
;
76 if (!exynos_gem
->dma_addr
) {
77 DRM_DEV_DEBUG_KMS(dev
->dev
, "dma_addr is invalid.\n");
81 DRM_DEV_DEBUG_KMS(dev
->dev
, "dma_addr(0x%lx), size(0x%lx)\n",
82 (unsigned long)exynos_gem
->dma_addr
, exynos_gem
->size
);
84 dma_free_attrs(to_dma_dev(dev
), exynos_gem
->size
, exynos_gem
->cookie
,
85 (dma_addr_t
)exynos_gem
->dma_addr
,
86 exynos_gem
->dma_attrs
);
89 static int exynos_drm_gem_handle_create(struct drm_gem_object
*obj
,
90 struct drm_file
*file_priv
,
96 * allocate a id of idr table where the obj is registered
97 * and handle has the id what user can see.
99 ret
= drm_gem_handle_create(file_priv
, obj
, handle
);
103 DRM_DEV_DEBUG_KMS(to_dma_dev(obj
->dev
), "gem handle = 0x%x\n", *handle
);
105 /* drop reference from allocate - handle holds it now. */
106 drm_gem_object_put(obj
);
111 void exynos_drm_gem_destroy(struct exynos_drm_gem
*exynos_gem
)
113 struct drm_gem_object
*obj
= &exynos_gem
->base
;
115 DRM_DEV_DEBUG_KMS(to_dma_dev(obj
->dev
), "handle count = %d\n",
119 * do not release memory region from exporter.
121 * the region will be released by exporter
122 * once dmabuf's refcount becomes 0.
124 if (obj
->import_attach
)
125 drm_prime_gem_destroy(obj
, exynos_gem
->sgt
);
127 exynos_drm_free_buf(exynos_gem
);
129 /* release file pointer to gem object. */
130 drm_gem_object_release(obj
);
135 static const struct vm_operations_struct exynos_drm_gem_vm_ops
= {
136 .open
= drm_gem_vm_open
,
137 .close
= drm_gem_vm_close
,
140 static const struct drm_gem_object_funcs exynos_drm_gem_object_funcs
= {
141 .free
= exynos_drm_gem_free_object
,
142 .get_sg_table
= exynos_drm_gem_prime_get_sg_table
,
143 .mmap
= exynos_drm_gem_mmap
,
144 .vm_ops
= &exynos_drm_gem_vm_ops
,
147 static struct exynos_drm_gem
*exynos_drm_gem_init(struct drm_device
*dev
,
150 struct exynos_drm_gem
*exynos_gem
;
151 struct drm_gem_object
*obj
;
154 exynos_gem
= kzalloc(sizeof(*exynos_gem
), GFP_KERNEL
);
156 return ERR_PTR(-ENOMEM
);
158 exynos_gem
->size
= size
;
159 obj
= &exynos_gem
->base
;
161 obj
->funcs
= &exynos_drm_gem_object_funcs
;
163 ret
= drm_gem_object_init(dev
, obj
, size
);
165 DRM_DEV_ERROR(dev
->dev
, "failed to initialize gem object\n");
170 ret
= drm_gem_create_mmap_offset(obj
);
172 drm_gem_object_release(obj
);
177 DRM_DEV_DEBUG_KMS(dev
->dev
, "created file object = %pK\n", obj
->filp
);
182 struct exynos_drm_gem
*exynos_drm_gem_create(struct drm_device
*dev
,
187 struct exynos_drm_gem
*exynos_gem
;
190 if (flags
& ~(EXYNOS_BO_MASK
)) {
191 DRM_DEV_ERROR(dev
->dev
,
192 "invalid GEM buffer flags: %u\n", flags
);
193 return ERR_PTR(-EINVAL
);
197 DRM_DEV_ERROR(dev
->dev
, "invalid GEM buffer size: %lu\n", size
);
198 return ERR_PTR(-EINVAL
);
201 size
= roundup(size
, PAGE_SIZE
);
203 exynos_gem
= exynos_drm_gem_init(dev
, size
);
204 if (IS_ERR(exynos_gem
))
207 if (!is_drm_iommu_supported(dev
) && (flags
& EXYNOS_BO_NONCONTIG
)) {
209 * when no IOMMU is available, all allocated buffers are
210 * contiguous anyway, so drop EXYNOS_BO_NONCONTIG flag
212 flags
&= ~EXYNOS_BO_NONCONTIG
;
213 DRM_WARN("Non-contiguous allocation is not supported without IOMMU, falling back to contiguous buffer\n");
216 /* set memory type and cache attribute from user side. */
217 exynos_gem
->flags
= flags
;
219 ret
= exynos_drm_alloc_buf(exynos_gem
, kvmap
);
221 drm_gem_object_release(&exynos_gem
->base
);
229 int exynos_drm_gem_create_ioctl(struct drm_device
*dev
, void *data
,
230 struct drm_file
*file_priv
)
232 struct drm_exynos_gem_create
*args
= data
;
233 struct exynos_drm_gem
*exynos_gem
;
236 exynos_gem
= exynos_drm_gem_create(dev
, args
->flags
, args
->size
, false);
237 if (IS_ERR(exynos_gem
))
238 return PTR_ERR(exynos_gem
);
240 ret
= exynos_drm_gem_handle_create(&exynos_gem
->base
, file_priv
,
243 exynos_drm_gem_destroy(exynos_gem
);
250 int exynos_drm_gem_map_ioctl(struct drm_device
*dev
, void *data
,
251 struct drm_file
*file_priv
)
253 struct drm_exynos_gem_map
*args
= data
;
255 return drm_gem_dumb_map_offset(file_priv
, dev
, args
->handle
,
259 struct exynos_drm_gem
*exynos_drm_gem_get(struct drm_file
*filp
,
260 unsigned int gem_handle
)
262 struct drm_gem_object
*obj
;
264 obj
= drm_gem_object_lookup(filp
, gem_handle
);
267 return to_exynos_gem(obj
);
270 static int exynos_drm_gem_mmap_buffer(struct exynos_drm_gem
*exynos_gem
,
271 struct vm_area_struct
*vma
)
273 struct drm_device
*drm_dev
= exynos_gem
->base
.dev
;
274 unsigned long vm_size
;
277 vm_flags_clear(vma
, VM_PFNMAP
);
280 vm_size
= vma
->vm_end
- vma
->vm_start
;
282 /* check if user-requested size is valid. */
283 if (vm_size
> exynos_gem
->size
)
286 ret
= dma_mmap_attrs(to_dma_dev(drm_dev
), vma
, exynos_gem
->cookie
,
287 exynos_gem
->dma_addr
, exynos_gem
->size
,
288 exynos_gem
->dma_attrs
);
290 DRM_ERROR("failed to mmap.\n");
297 int exynos_drm_gem_get_ioctl(struct drm_device
*dev
, void *data
,
298 struct drm_file
*file_priv
)
300 struct exynos_drm_gem
*exynos_gem
;
301 struct drm_exynos_gem_info
*args
= data
;
302 struct drm_gem_object
*obj
;
304 obj
= drm_gem_object_lookup(file_priv
, args
->handle
);
306 DRM_DEV_ERROR(dev
->dev
, "failed to lookup gem object.\n");
310 exynos_gem
= to_exynos_gem(obj
);
312 args
->flags
= exynos_gem
->flags
;
313 args
->size
= exynos_gem
->size
;
315 drm_gem_object_put(obj
);
320 void exynos_drm_gem_free_object(struct drm_gem_object
*obj
)
322 exynos_drm_gem_destroy(to_exynos_gem(obj
));
325 int exynos_drm_gem_dumb_create(struct drm_file
*file_priv
,
326 struct drm_device
*dev
,
327 struct drm_mode_create_dumb
*args
)
329 struct exynos_drm_gem
*exynos_gem
;
334 * allocate memory to be used for framebuffer.
335 * - this callback would be called by user application
336 * with DRM_IOCTL_MODE_CREATE_DUMB command.
339 args
->pitch
= args
->width
* ((args
->bpp
+ 7) / 8);
340 args
->size
= args
->pitch
* args
->height
;
342 if (is_drm_iommu_supported(dev
))
343 flags
= EXYNOS_BO_NONCONTIG
| EXYNOS_BO_WC
;
345 flags
= EXYNOS_BO_CONTIG
| EXYNOS_BO_WC
;
347 exynos_gem
= exynos_drm_gem_create(dev
, flags
, args
->size
, false);
348 if (IS_ERR(exynos_gem
)) {
349 dev_warn(dev
->dev
, "FB allocation failed.\n");
350 return PTR_ERR(exynos_gem
);
353 ret
= exynos_drm_gem_handle_create(&exynos_gem
->base
, file_priv
,
356 exynos_drm_gem_destroy(exynos_gem
);
363 static int exynos_drm_gem_mmap(struct drm_gem_object
*obj
, struct vm_area_struct
*vma
)
365 struct exynos_drm_gem
*exynos_gem
= to_exynos_gem(obj
);
368 if (obj
->import_attach
)
369 return dma_buf_mmap(obj
->dma_buf
, vma
, 0);
371 vm_flags_set(vma
, VM_IO
| VM_DONTEXPAND
| VM_DONTDUMP
);
373 DRM_DEV_DEBUG_KMS(to_dma_dev(obj
->dev
), "flags = 0x%x\n",
376 /* non-cachable as default. */
377 if (exynos_gem
->flags
& EXYNOS_BO_CACHABLE
)
378 vma
->vm_page_prot
= vm_get_page_prot(vma
->vm_flags
);
379 else if (exynos_gem
->flags
& EXYNOS_BO_WC
)
381 pgprot_writecombine(vm_get_page_prot(vma
->vm_flags
));
384 pgprot_noncached(vm_get_page_prot(vma
->vm_flags
));
386 ret
= exynos_drm_gem_mmap_buffer(exynos_gem
, vma
);
393 drm_gem_vm_close(vma
);
398 /* low-level interface prime helpers */
399 struct drm_gem_object
*exynos_drm_gem_prime_import(struct drm_device
*dev
,
400 struct dma_buf
*dma_buf
)
402 return drm_gem_prime_import_dev(dev
, dma_buf
, to_dma_dev(dev
));
405 struct sg_table
*exynos_drm_gem_prime_get_sg_table(struct drm_gem_object
*obj
)
407 struct exynos_drm_gem
*exynos_gem
= to_exynos_gem(obj
);
408 struct drm_device
*drm_dev
= obj
->dev
;
409 struct sg_table
*sgt
;
412 sgt
= kzalloc(sizeof(*sgt
), GFP_KERNEL
);
414 return ERR_PTR(-ENOMEM
);
416 ret
= dma_get_sgtable_attrs(to_dma_dev(drm_dev
), sgt
, exynos_gem
->cookie
,
417 exynos_gem
->dma_addr
, exynos_gem
->size
,
418 exynos_gem
->dma_attrs
);
420 DRM_ERROR("failed to get sgtable, %d\n", ret
);
428 struct drm_gem_object
*
429 exynos_drm_gem_prime_import_sg_table(struct drm_device
*dev
,
430 struct dma_buf_attachment
*attach
,
431 struct sg_table
*sgt
)
433 struct exynos_drm_gem
*exynos_gem
;
435 /* check if the entries in the sg_table are contiguous */
436 if (drm_prime_get_contiguous_size(sgt
) < attach
->dmabuf
->size
) {
437 DRM_ERROR("buffer chunks must be mapped contiguously");
438 return ERR_PTR(-EINVAL
);
441 exynos_gem
= exynos_drm_gem_init(dev
, attach
->dmabuf
->size
);
442 if (IS_ERR(exynos_gem
))
443 return ERR_CAST(exynos_gem
);
446 * Buffer has been mapped as contiguous into DMA address space,
447 * but if there is IOMMU, it can be either CONTIG or NONCONTIG.
448 * We assume a simplified logic below:
450 if (is_drm_iommu_supported(dev
))
451 exynos_gem
->flags
|= EXYNOS_BO_NONCONTIG
;
453 exynos_gem
->flags
|= EXYNOS_BO_CONTIG
;
455 exynos_gem
->dma_addr
= sg_dma_address(sgt
->sgl
);
456 exynos_gem
->sgt
= sgt
;
457 return &exynos_gem
->base
;