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>
13 #include <drm/drm_prime.h>
14 #include <drm/drm_vma_manager.h>
15 #include <drm/exynos_drm.h>
17 #include "exynos_drm_drv.h"
18 #include "exynos_drm_gem.h"
20 static int exynos_drm_alloc_buf(struct exynos_drm_gem
*exynos_gem
, bool kvmap
)
22 struct drm_device
*dev
= exynos_gem
->base
.dev
;
23 unsigned long attr
= 0;
25 if (exynos_gem
->dma_addr
) {
26 DRM_DEV_DEBUG_KMS(to_dma_dev(dev
), "already allocated.\n");
31 * if EXYNOS_BO_CONTIG, fully physically contiguous memory
32 * region will be allocated else physically contiguous
35 if (!(exynos_gem
->flags
& EXYNOS_BO_NONCONTIG
))
36 attr
|= DMA_ATTR_FORCE_CONTIGUOUS
;
39 * if EXYNOS_BO_WC or EXYNOS_BO_NONCACHABLE, writecombine mapping
40 * else cachable mapping.
42 if (exynos_gem
->flags
& EXYNOS_BO_WC
||
43 !(exynos_gem
->flags
& EXYNOS_BO_CACHABLE
))
44 attr
|= DMA_ATTR_WRITE_COMBINE
;
46 /* FBDev emulation requires kernel mapping */
48 attr
|= DMA_ATTR_NO_KERNEL_MAPPING
;
50 exynos_gem
->dma_attrs
= attr
;
51 exynos_gem
->cookie
= dma_alloc_attrs(to_dma_dev(dev
), exynos_gem
->size
,
52 &exynos_gem
->dma_addr
, GFP_KERNEL
,
53 exynos_gem
->dma_attrs
);
54 if (!exynos_gem
->cookie
) {
55 DRM_DEV_ERROR(to_dma_dev(dev
), "failed to allocate buffer.\n");
60 exynos_gem
->kvaddr
= exynos_gem
->cookie
;
62 DRM_DEV_DEBUG_KMS(to_dma_dev(dev
), "dma_addr(0x%lx), size(0x%lx)\n",
63 (unsigned long)exynos_gem
->dma_addr
, exynos_gem
->size
);
67 static void exynos_drm_free_buf(struct exynos_drm_gem
*exynos_gem
)
69 struct drm_device
*dev
= exynos_gem
->base
.dev
;
71 if (!exynos_gem
->dma_addr
) {
72 DRM_DEV_DEBUG_KMS(dev
->dev
, "dma_addr is invalid.\n");
76 DRM_DEV_DEBUG_KMS(dev
->dev
, "dma_addr(0x%lx), size(0x%lx)\n",
77 (unsigned long)exynos_gem
->dma_addr
, exynos_gem
->size
);
79 dma_free_attrs(to_dma_dev(dev
), exynos_gem
->size
, exynos_gem
->cookie
,
80 (dma_addr_t
)exynos_gem
->dma_addr
,
81 exynos_gem
->dma_attrs
);
84 static int exynos_drm_gem_handle_create(struct drm_gem_object
*obj
,
85 struct drm_file
*file_priv
,
91 * allocate a id of idr table where the obj is registered
92 * and handle has the id what user can see.
94 ret
= drm_gem_handle_create(file_priv
, obj
, handle
);
98 DRM_DEV_DEBUG_KMS(to_dma_dev(obj
->dev
), "gem handle = 0x%x\n", *handle
);
100 /* drop reference from allocate - handle holds it now. */
101 drm_gem_object_put(obj
);
106 void exynos_drm_gem_destroy(struct exynos_drm_gem
*exynos_gem
)
108 struct drm_gem_object
*obj
= &exynos_gem
->base
;
110 DRM_DEV_DEBUG_KMS(to_dma_dev(obj
->dev
), "handle count = %d\n",
114 * do not release memory region from exporter.
116 * the region will be released by exporter
117 * once dmabuf's refcount becomes 0.
119 if (obj
->import_attach
)
120 drm_prime_gem_destroy(obj
, exynos_gem
->sgt
);
122 exynos_drm_free_buf(exynos_gem
);
124 /* release file pointer to gem object. */
125 drm_gem_object_release(obj
);
130 static const struct vm_operations_struct exynos_drm_gem_vm_ops
= {
131 .open
= drm_gem_vm_open
,
132 .close
= drm_gem_vm_close
,
135 static const struct drm_gem_object_funcs exynos_drm_gem_object_funcs
= {
136 .free
= exynos_drm_gem_free_object
,
137 .get_sg_table
= exynos_drm_gem_prime_get_sg_table
,
138 .vm_ops
= &exynos_drm_gem_vm_ops
,
141 static struct exynos_drm_gem
*exynos_drm_gem_init(struct drm_device
*dev
,
144 struct exynos_drm_gem
*exynos_gem
;
145 struct drm_gem_object
*obj
;
148 exynos_gem
= kzalloc(sizeof(*exynos_gem
), GFP_KERNEL
);
150 return ERR_PTR(-ENOMEM
);
152 exynos_gem
->size
= size
;
153 obj
= &exynos_gem
->base
;
155 obj
->funcs
= &exynos_drm_gem_object_funcs
;
157 ret
= drm_gem_object_init(dev
, obj
, size
);
159 DRM_DEV_ERROR(dev
->dev
, "failed to initialize gem object\n");
164 ret
= drm_gem_create_mmap_offset(obj
);
166 drm_gem_object_release(obj
);
171 DRM_DEV_DEBUG_KMS(dev
->dev
, "created file object = %pK\n", obj
->filp
);
176 struct exynos_drm_gem
*exynos_drm_gem_create(struct drm_device
*dev
,
181 struct exynos_drm_gem
*exynos_gem
;
184 if (flags
& ~(EXYNOS_BO_MASK
)) {
185 DRM_DEV_ERROR(dev
->dev
,
186 "invalid GEM buffer flags: %u\n", flags
);
187 return ERR_PTR(-EINVAL
);
191 DRM_DEV_ERROR(dev
->dev
, "invalid GEM buffer size: %lu\n", size
);
192 return ERR_PTR(-EINVAL
);
195 size
= roundup(size
, PAGE_SIZE
);
197 exynos_gem
= exynos_drm_gem_init(dev
, size
);
198 if (IS_ERR(exynos_gem
))
201 if (!is_drm_iommu_supported(dev
) && (flags
& EXYNOS_BO_NONCONTIG
)) {
203 * when no IOMMU is available, all allocated buffers are
204 * contiguous anyway, so drop EXYNOS_BO_NONCONTIG flag
206 flags
&= ~EXYNOS_BO_NONCONTIG
;
207 DRM_WARN("Non-contiguous allocation is not supported without IOMMU, falling back to contiguous buffer\n");
210 /* set memory type and cache attribute from user side. */
211 exynos_gem
->flags
= flags
;
213 ret
= exynos_drm_alloc_buf(exynos_gem
, kvmap
);
215 drm_gem_object_release(&exynos_gem
->base
);
223 int exynos_drm_gem_create_ioctl(struct drm_device
*dev
, void *data
,
224 struct drm_file
*file_priv
)
226 struct drm_exynos_gem_create
*args
= data
;
227 struct exynos_drm_gem
*exynos_gem
;
230 exynos_gem
= exynos_drm_gem_create(dev
, args
->flags
, args
->size
, false);
231 if (IS_ERR(exynos_gem
))
232 return PTR_ERR(exynos_gem
);
234 ret
= exynos_drm_gem_handle_create(&exynos_gem
->base
, file_priv
,
237 exynos_drm_gem_destroy(exynos_gem
);
244 int exynos_drm_gem_map_ioctl(struct drm_device
*dev
, void *data
,
245 struct drm_file
*file_priv
)
247 struct drm_exynos_gem_map
*args
= data
;
249 return drm_gem_dumb_map_offset(file_priv
, dev
, args
->handle
,
253 struct exynos_drm_gem
*exynos_drm_gem_get(struct drm_file
*filp
,
254 unsigned int gem_handle
)
256 struct drm_gem_object
*obj
;
258 obj
= drm_gem_object_lookup(filp
, gem_handle
);
261 return to_exynos_gem(obj
);
264 static int exynos_drm_gem_mmap_buffer(struct exynos_drm_gem
*exynos_gem
,
265 struct vm_area_struct
*vma
)
267 struct drm_device
*drm_dev
= exynos_gem
->base
.dev
;
268 unsigned long vm_size
;
271 vma
->vm_flags
&= ~VM_PFNMAP
;
274 vm_size
= vma
->vm_end
- vma
->vm_start
;
276 /* check if user-requested size is valid. */
277 if (vm_size
> exynos_gem
->size
)
280 ret
= dma_mmap_attrs(to_dma_dev(drm_dev
), vma
, exynos_gem
->cookie
,
281 exynos_gem
->dma_addr
, exynos_gem
->size
,
282 exynos_gem
->dma_attrs
);
284 DRM_ERROR("failed to mmap.\n");
291 int exynos_drm_gem_get_ioctl(struct drm_device
*dev
, void *data
,
292 struct drm_file
*file_priv
)
294 struct exynos_drm_gem
*exynos_gem
;
295 struct drm_exynos_gem_info
*args
= data
;
296 struct drm_gem_object
*obj
;
298 obj
= drm_gem_object_lookup(file_priv
, args
->handle
);
300 DRM_DEV_ERROR(dev
->dev
, "failed to lookup gem object.\n");
304 exynos_gem
= to_exynos_gem(obj
);
306 args
->flags
= exynos_gem
->flags
;
307 args
->size
= exynos_gem
->size
;
309 drm_gem_object_put(obj
);
314 void exynos_drm_gem_free_object(struct drm_gem_object
*obj
)
316 exynos_drm_gem_destroy(to_exynos_gem(obj
));
319 int exynos_drm_gem_dumb_create(struct drm_file
*file_priv
,
320 struct drm_device
*dev
,
321 struct drm_mode_create_dumb
*args
)
323 struct exynos_drm_gem
*exynos_gem
;
328 * allocate memory to be used for framebuffer.
329 * - this callback would be called by user application
330 * with DRM_IOCTL_MODE_CREATE_DUMB command.
333 args
->pitch
= args
->width
* ((args
->bpp
+ 7) / 8);
334 args
->size
= args
->pitch
* args
->height
;
336 if (is_drm_iommu_supported(dev
))
337 flags
= EXYNOS_BO_NONCONTIG
| EXYNOS_BO_WC
;
339 flags
= EXYNOS_BO_CONTIG
| EXYNOS_BO_WC
;
341 exynos_gem
= exynos_drm_gem_create(dev
, flags
, args
->size
, false);
342 if (IS_ERR(exynos_gem
)) {
343 dev_warn(dev
->dev
, "FB allocation failed.\n");
344 return PTR_ERR(exynos_gem
);
347 ret
= exynos_drm_gem_handle_create(&exynos_gem
->base
, file_priv
,
350 exynos_drm_gem_destroy(exynos_gem
);
357 static int exynos_drm_gem_mmap_obj(struct drm_gem_object
*obj
,
358 struct vm_area_struct
*vma
)
360 struct exynos_drm_gem
*exynos_gem
= to_exynos_gem(obj
);
363 DRM_DEV_DEBUG_KMS(to_dma_dev(obj
->dev
), "flags = 0x%x\n",
366 /* non-cachable as default. */
367 if (exynos_gem
->flags
& EXYNOS_BO_CACHABLE
)
368 vma
->vm_page_prot
= vm_get_page_prot(vma
->vm_flags
);
369 else if (exynos_gem
->flags
& EXYNOS_BO_WC
)
371 pgprot_writecombine(vm_get_page_prot(vma
->vm_flags
));
374 pgprot_noncached(vm_get_page_prot(vma
->vm_flags
));
376 ret
= exynos_drm_gem_mmap_buffer(exynos_gem
, vma
);
383 drm_gem_vm_close(vma
);
388 int exynos_drm_gem_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
390 struct drm_gem_object
*obj
;
393 /* set vm_area_struct. */
394 ret
= drm_gem_mmap(filp
, vma
);
396 DRM_ERROR("failed to mmap.\n");
400 obj
= vma
->vm_private_data
;
402 if (obj
->import_attach
)
403 return dma_buf_mmap(obj
->dma_buf
, vma
, 0);
405 return exynos_drm_gem_mmap_obj(obj
, vma
);
408 /* low-level interface prime helpers */
409 struct drm_gem_object
*exynos_drm_gem_prime_import(struct drm_device
*dev
,
410 struct dma_buf
*dma_buf
)
412 return drm_gem_prime_import_dev(dev
, dma_buf
, to_dma_dev(dev
));
415 struct sg_table
*exynos_drm_gem_prime_get_sg_table(struct drm_gem_object
*obj
)
417 struct exynos_drm_gem
*exynos_gem
= to_exynos_gem(obj
);
418 struct drm_device
*drm_dev
= obj
->dev
;
419 struct sg_table
*sgt
;
422 sgt
= kzalloc(sizeof(*sgt
), GFP_KERNEL
);
424 return ERR_PTR(-ENOMEM
);
426 ret
= dma_get_sgtable_attrs(to_dma_dev(drm_dev
), sgt
, exynos_gem
->cookie
,
427 exynos_gem
->dma_addr
, exynos_gem
->size
,
428 exynos_gem
->dma_attrs
);
430 DRM_ERROR("failed to get sgtable, %d\n", ret
);
438 struct drm_gem_object
*
439 exynos_drm_gem_prime_import_sg_table(struct drm_device
*dev
,
440 struct dma_buf_attachment
*attach
,
441 struct sg_table
*sgt
)
443 struct exynos_drm_gem
*exynos_gem
;
445 /* check if the entries in the sg_table are contiguous */
446 if (drm_prime_get_contiguous_size(sgt
) < attach
->dmabuf
->size
) {
447 DRM_ERROR("buffer chunks must be mapped contiguously");
448 return ERR_PTR(-EINVAL
);
451 exynos_gem
= exynos_drm_gem_init(dev
, attach
->dmabuf
->size
);
452 if (IS_ERR(exynos_gem
))
453 return ERR_CAST(exynos_gem
);
456 * Buffer has been mapped as contiguous into DMA address space,
457 * but if there is IOMMU, it can be either CONTIG or NONCONTIG.
458 * We assume a simplified logic below:
460 if (is_drm_iommu_supported(dev
))
461 exynos_gem
->flags
|= EXYNOS_BO_NONCONTIG
;
463 exynos_gem
->flags
|= EXYNOS_BO_CONTIG
;
465 exynos_gem
->dma_addr
= sg_dma_address(sgt
->sgl
);
466 exynos_gem
->sgt
= sgt
;
467 return &exynos_gem
->base
;
470 int exynos_drm_gem_prime_mmap(struct drm_gem_object
*obj
,
471 struct vm_area_struct
*vma
)
475 ret
= drm_gem_mmap_obj(obj
, obj
->size
, vma
);
479 return exynos_drm_gem_mmap_obj(obj
, vma
);