3 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * Author: Inki Dae <inki.dae@samsung.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
13 #include <drm/drm_vma_manager.h>
15 #include <linux/shmem_fs.h>
16 #include <linux/dma-buf.h>
17 #include <linux/pfn_t.h>
18 #include <drm/exynos_drm.h>
20 #include "exynos_drm_drv.h"
21 #include "exynos_drm_gem.h"
22 #include "exynos_drm_iommu.h"
24 static int exynos_drm_alloc_buf(struct exynos_drm_gem
*exynos_gem
)
26 struct drm_device
*dev
= exynos_gem
->base
.dev
;
28 unsigned int nr_pages
;
32 if (exynos_gem
->dma_addr
) {
33 DRM_DEBUG_KMS("already allocated.\n");
37 exynos_gem
->dma_attrs
= 0;
40 * if EXYNOS_BO_CONTIG, fully physically contiguous memory
41 * region will be allocated else physically contiguous
44 if (!(exynos_gem
->flags
& EXYNOS_BO_NONCONTIG
))
45 exynos_gem
->dma_attrs
|= DMA_ATTR_FORCE_CONTIGUOUS
;
48 * if EXYNOS_BO_WC or EXYNOS_BO_NONCACHABLE, writecombine mapping
49 * else cachable mapping.
51 if (exynos_gem
->flags
& EXYNOS_BO_WC
||
52 !(exynos_gem
->flags
& EXYNOS_BO_CACHABLE
))
53 attr
= DMA_ATTR_WRITE_COMBINE
;
55 attr
= DMA_ATTR_NON_CONSISTENT
;
57 exynos_gem
->dma_attrs
|= attr
;
58 exynos_gem
->dma_attrs
|= DMA_ATTR_NO_KERNEL_MAPPING
;
60 nr_pages
= exynos_gem
->size
>> PAGE_SHIFT
;
62 exynos_gem
->pages
= drm_calloc_large(nr_pages
, sizeof(struct page
*));
63 if (!exynos_gem
->pages
) {
64 DRM_ERROR("failed to allocate pages.\n");
68 exynos_gem
->cookie
= dma_alloc_attrs(to_dma_dev(dev
), exynos_gem
->size
,
69 &exynos_gem
->dma_addr
, GFP_KERNEL
,
70 exynos_gem
->dma_attrs
);
71 if (!exynos_gem
->cookie
) {
72 DRM_ERROR("failed to allocate buffer.\n");
76 ret
= dma_get_sgtable_attrs(to_dma_dev(dev
), &sgt
, exynos_gem
->cookie
,
77 exynos_gem
->dma_addr
, exynos_gem
->size
,
78 exynos_gem
->dma_attrs
);
80 DRM_ERROR("failed to get sgtable.\n");
84 if (drm_prime_sg_to_page_addr_arrays(&sgt
, exynos_gem
->pages
, NULL
,
86 DRM_ERROR("invalid sgtable.\n");
93 DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
94 (unsigned long)exynos_gem
->dma_addr
, exynos_gem
->size
);
101 dma_free_attrs(to_dma_dev(dev
), exynos_gem
->size
, exynos_gem
->cookie
,
102 exynos_gem
->dma_addr
, exynos_gem
->dma_attrs
);
104 drm_free_large(exynos_gem
->pages
);
109 static void exynos_drm_free_buf(struct exynos_drm_gem
*exynos_gem
)
111 struct drm_device
*dev
= exynos_gem
->base
.dev
;
113 if (!exynos_gem
->dma_addr
) {
114 DRM_DEBUG_KMS("dma_addr is invalid.\n");
118 DRM_DEBUG_KMS("dma_addr(0x%lx), size(0x%lx)\n",
119 (unsigned long)exynos_gem
->dma_addr
, exynos_gem
->size
);
121 dma_free_attrs(to_dma_dev(dev
), exynos_gem
->size
, exynos_gem
->cookie
,
122 (dma_addr_t
)exynos_gem
->dma_addr
,
123 exynos_gem
->dma_attrs
);
125 drm_free_large(exynos_gem
->pages
);
128 static int exynos_drm_gem_handle_create(struct drm_gem_object
*obj
,
129 struct drm_file
*file_priv
,
130 unsigned int *handle
)
135 * allocate a id of idr table where the obj is registered
136 * and handle has the id what user can see.
138 ret
= drm_gem_handle_create(file_priv
, obj
, handle
);
142 DRM_DEBUG_KMS("gem handle = 0x%x\n", *handle
);
144 /* drop reference from allocate - handle holds it now. */
145 drm_gem_object_unreference_unlocked(obj
);
150 void exynos_drm_gem_destroy(struct exynos_drm_gem
*exynos_gem
)
152 struct drm_gem_object
*obj
= &exynos_gem
->base
;
154 DRM_DEBUG_KMS("handle count = %d\n", obj
->handle_count
);
157 * do not release memory region from exporter.
159 * the region will be released by exporter
160 * once dmabuf's refcount becomes 0.
162 if (obj
->import_attach
)
163 drm_prime_gem_destroy(obj
, exynos_gem
->sgt
);
165 exynos_drm_free_buf(exynos_gem
);
167 /* release file pointer to gem object. */
168 drm_gem_object_release(obj
);
173 unsigned long exynos_drm_gem_get_size(struct drm_device
*dev
,
174 unsigned int gem_handle
,
175 struct drm_file
*file_priv
)
177 struct exynos_drm_gem
*exynos_gem
;
178 struct drm_gem_object
*obj
;
180 obj
= drm_gem_object_lookup(file_priv
, gem_handle
);
182 DRM_ERROR("failed to lookup gem object.\n");
186 exynos_gem
= to_exynos_gem(obj
);
188 drm_gem_object_unreference_unlocked(obj
);
190 return exynos_gem
->size
;
193 static struct exynos_drm_gem
*exynos_drm_gem_init(struct drm_device
*dev
,
196 struct exynos_drm_gem
*exynos_gem
;
197 struct drm_gem_object
*obj
;
200 exynos_gem
= kzalloc(sizeof(*exynos_gem
), GFP_KERNEL
);
202 return ERR_PTR(-ENOMEM
);
204 exynos_gem
->size
= size
;
205 obj
= &exynos_gem
->base
;
207 ret
= drm_gem_object_init(dev
, obj
, size
);
209 DRM_ERROR("failed to initialize gem object\n");
214 ret
= drm_gem_create_mmap_offset(obj
);
216 drm_gem_object_release(obj
);
221 DRM_DEBUG_KMS("created file object = %p\n", obj
->filp
);
226 struct exynos_drm_gem
*exynos_drm_gem_create(struct drm_device
*dev
,
230 struct exynos_drm_gem
*exynos_gem
;
233 if (flags
& ~(EXYNOS_BO_MASK
)) {
234 DRM_ERROR("invalid flags.\n");
235 return ERR_PTR(-EINVAL
);
239 DRM_ERROR("invalid size.\n");
240 return ERR_PTR(-EINVAL
);
243 size
= roundup(size
, PAGE_SIZE
);
245 exynos_gem
= exynos_drm_gem_init(dev
, size
);
246 if (IS_ERR(exynos_gem
))
249 if (!is_drm_iommu_supported(dev
) && (flags
& EXYNOS_BO_NONCONTIG
)) {
251 * when no IOMMU is available, all allocated buffers are
252 * contiguous anyway, so drop EXYNOS_BO_NONCONTIG flag
254 flags
&= ~EXYNOS_BO_NONCONTIG
;
255 DRM_WARN("Non-contiguous allocation is not supported without IOMMU, falling back to contiguous buffer\n");
258 /* set memory type and cache attribute from user side. */
259 exynos_gem
->flags
= flags
;
261 ret
= exynos_drm_alloc_buf(exynos_gem
);
263 drm_gem_object_release(&exynos_gem
->base
);
271 int exynos_drm_gem_create_ioctl(struct drm_device
*dev
, void *data
,
272 struct drm_file
*file_priv
)
274 struct drm_exynos_gem_create
*args
= data
;
275 struct exynos_drm_gem
*exynos_gem
;
278 exynos_gem
= exynos_drm_gem_create(dev
, args
->flags
, args
->size
);
279 if (IS_ERR(exynos_gem
))
280 return PTR_ERR(exynos_gem
);
282 ret
= exynos_drm_gem_handle_create(&exynos_gem
->base
, file_priv
,
285 exynos_drm_gem_destroy(exynos_gem
);
292 int exynos_drm_gem_map_ioctl(struct drm_device
*dev
, void *data
,
293 struct drm_file
*file_priv
)
295 struct drm_exynos_gem_map
*args
= data
;
297 return exynos_drm_gem_dumb_map_offset(file_priv
, dev
, args
->handle
,
301 dma_addr_t
*exynos_drm_gem_get_dma_addr(struct drm_device
*dev
,
302 unsigned int gem_handle
,
303 struct drm_file
*filp
)
305 struct exynos_drm_gem
*exynos_gem
;
306 struct drm_gem_object
*obj
;
308 obj
= drm_gem_object_lookup(filp
, gem_handle
);
310 DRM_ERROR("failed to lookup gem object.\n");
311 return ERR_PTR(-EINVAL
);
314 exynos_gem
= to_exynos_gem(obj
);
316 return &exynos_gem
->dma_addr
;
319 void exynos_drm_gem_put_dma_addr(struct drm_device
*dev
,
320 unsigned int gem_handle
,
321 struct drm_file
*filp
)
323 struct drm_gem_object
*obj
;
325 obj
= drm_gem_object_lookup(filp
, gem_handle
);
327 DRM_ERROR("failed to lookup gem object.\n");
331 drm_gem_object_unreference_unlocked(obj
);
334 * decrease obj->refcount one more time because we has already
335 * increased it at exynos_drm_gem_get_dma_addr().
337 drm_gem_object_unreference_unlocked(obj
);
340 static int exynos_drm_gem_mmap_buffer(struct exynos_drm_gem
*exynos_gem
,
341 struct vm_area_struct
*vma
)
343 struct drm_device
*drm_dev
= exynos_gem
->base
.dev
;
344 unsigned long vm_size
;
347 vma
->vm_flags
&= ~VM_PFNMAP
;
350 vm_size
= vma
->vm_end
- vma
->vm_start
;
352 /* check if user-requested size is valid. */
353 if (vm_size
> exynos_gem
->size
)
356 ret
= dma_mmap_attrs(to_dma_dev(drm_dev
), vma
, exynos_gem
->cookie
,
357 exynos_gem
->dma_addr
, exynos_gem
->size
,
358 exynos_gem
->dma_attrs
);
360 DRM_ERROR("failed to mmap.\n");
367 int exynos_drm_gem_get_ioctl(struct drm_device
*dev
, void *data
,
368 struct drm_file
*file_priv
)
370 struct exynos_drm_gem
*exynos_gem
;
371 struct drm_exynos_gem_info
*args
= data
;
372 struct drm_gem_object
*obj
;
374 obj
= drm_gem_object_lookup(file_priv
, args
->handle
);
376 DRM_ERROR("failed to lookup gem object.\n");
380 exynos_gem
= to_exynos_gem(obj
);
382 args
->flags
= exynos_gem
->flags
;
383 args
->size
= exynos_gem
->size
;
385 drm_gem_object_unreference_unlocked(obj
);
390 void exynos_drm_gem_free_object(struct drm_gem_object
*obj
)
392 exynos_drm_gem_destroy(to_exynos_gem(obj
));
395 int exynos_drm_gem_dumb_create(struct drm_file
*file_priv
,
396 struct drm_device
*dev
,
397 struct drm_mode_create_dumb
*args
)
399 struct exynos_drm_gem
*exynos_gem
;
404 * allocate memory to be used for framebuffer.
405 * - this callback would be called by user application
406 * with DRM_IOCTL_MODE_CREATE_DUMB command.
409 args
->pitch
= args
->width
* ((args
->bpp
+ 7) / 8);
410 args
->size
= args
->pitch
* args
->height
;
412 if (is_drm_iommu_supported(dev
))
413 flags
= EXYNOS_BO_NONCONTIG
| EXYNOS_BO_WC
;
415 flags
= EXYNOS_BO_CONTIG
| EXYNOS_BO_WC
;
417 exynos_gem
= exynos_drm_gem_create(dev
, flags
, args
->size
);
418 if (IS_ERR(exynos_gem
)) {
419 dev_warn(dev
->dev
, "FB allocation failed.\n");
420 return PTR_ERR(exynos_gem
);
423 ret
= exynos_drm_gem_handle_create(&exynos_gem
->base
, file_priv
,
426 exynos_drm_gem_destroy(exynos_gem
);
433 int exynos_drm_gem_dumb_map_offset(struct drm_file
*file_priv
,
434 struct drm_device
*dev
, uint32_t handle
,
437 struct drm_gem_object
*obj
;
441 * get offset of memory allocated for drm framebuffer.
442 * - this callback would be called by user application
443 * with DRM_IOCTL_MODE_MAP_DUMB command.
446 obj
= drm_gem_object_lookup(file_priv
, handle
);
448 DRM_ERROR("failed to lookup gem object.\n");
452 *offset
= drm_vma_node_offset_addr(&obj
->vma_node
);
453 DRM_DEBUG_KMS("offset = 0x%lx\n", (unsigned long)*offset
);
455 drm_gem_object_unreference_unlocked(obj
);
459 int exynos_drm_gem_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
461 struct drm_gem_object
*obj
= vma
->vm_private_data
;
462 struct exynos_drm_gem
*exynos_gem
= to_exynos_gem(obj
);
467 page_offset
= ((unsigned long)vmf
->virtual_address
-
468 vma
->vm_start
) >> PAGE_SHIFT
;
470 if (page_offset
>= (exynos_gem
->size
>> PAGE_SHIFT
)) {
471 DRM_ERROR("invalid page offset\n");
476 pfn
= page_to_pfn(exynos_gem
->pages
[page_offset
]);
477 ret
= vm_insert_mixed(vma
, (unsigned long)vmf
->virtual_address
,
478 __pfn_to_pfn_t(pfn
, PFN_DEV
));
485 return VM_FAULT_NOPAGE
;
489 return VM_FAULT_SIGBUS
;
493 static int exynos_drm_gem_mmap_obj(struct drm_gem_object
*obj
,
494 struct vm_area_struct
*vma
)
496 struct exynos_drm_gem
*exynos_gem
= to_exynos_gem(obj
);
499 DRM_DEBUG_KMS("flags = 0x%x\n", exynos_gem
->flags
);
501 /* non-cachable as default. */
502 if (exynos_gem
->flags
& EXYNOS_BO_CACHABLE
)
503 vma
->vm_page_prot
= vm_get_page_prot(vma
->vm_flags
);
504 else if (exynos_gem
->flags
& EXYNOS_BO_WC
)
506 pgprot_writecombine(vm_get_page_prot(vma
->vm_flags
));
509 pgprot_noncached(vm_get_page_prot(vma
->vm_flags
));
511 ret
= exynos_drm_gem_mmap_buffer(exynos_gem
, vma
);
518 drm_gem_vm_close(vma
);
523 int exynos_drm_gem_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
525 struct drm_gem_object
*obj
;
528 /* set vm_area_struct. */
529 ret
= drm_gem_mmap(filp
, vma
);
531 DRM_ERROR("failed to mmap.\n");
535 obj
= vma
->vm_private_data
;
537 if (obj
->import_attach
)
538 return dma_buf_mmap(obj
->dma_buf
, vma
, 0);
540 return exynos_drm_gem_mmap_obj(obj
, vma
);
543 /* low-level interface prime helpers */
544 struct sg_table
*exynos_drm_gem_prime_get_sg_table(struct drm_gem_object
*obj
)
546 struct exynos_drm_gem
*exynos_gem
= to_exynos_gem(obj
);
549 npages
= exynos_gem
->size
>> PAGE_SHIFT
;
551 return drm_prime_pages_to_sg(exynos_gem
->pages
, npages
);
554 struct drm_gem_object
*
555 exynos_drm_gem_prime_import_sg_table(struct drm_device
*dev
,
556 struct dma_buf_attachment
*attach
,
557 struct sg_table
*sgt
)
559 struct exynos_drm_gem
*exynos_gem
;
563 exynos_gem
= exynos_drm_gem_init(dev
, attach
->dmabuf
->size
);
564 if (IS_ERR(exynos_gem
)) {
565 ret
= PTR_ERR(exynos_gem
);
569 exynos_gem
->dma_addr
= sg_dma_address(sgt
->sgl
);
571 npages
= exynos_gem
->size
>> PAGE_SHIFT
;
572 exynos_gem
->pages
= drm_malloc_ab(npages
, sizeof(struct page
*));
573 if (!exynos_gem
->pages
) {
578 ret
= drm_prime_sg_to_page_addr_arrays(sgt
, exynos_gem
->pages
, NULL
,
583 exynos_gem
->sgt
= sgt
;
585 if (sgt
->nents
== 1) {
586 /* always physically continuous memory if sgt->nents is 1. */
587 exynos_gem
->flags
|= EXYNOS_BO_CONTIG
;
590 * this case could be CONTIG or NONCONTIG type but for now
592 * TODO. we have to find a way that exporter can notify
593 * the type of its own buffer to importer.
595 exynos_gem
->flags
|= EXYNOS_BO_NONCONTIG
;
598 return &exynos_gem
->base
;
601 drm_free_large(exynos_gem
->pages
);
603 drm_gem_object_release(&exynos_gem
->base
);
608 void *exynos_drm_gem_prime_vmap(struct drm_gem_object
*obj
)
613 void exynos_drm_gem_prime_vunmap(struct drm_gem_object
*obj
, void *vaddr
)
618 int exynos_drm_gem_prime_mmap(struct drm_gem_object
*obj
,
619 struct vm_area_struct
*vma
)
623 ret
= drm_gem_mmap_obj(obj
, obj
->size
, vma
);
627 return exynos_drm_gem_mmap_obj(obj
, vma
);