1 /* exynos_drm_gem.h
2  *
3  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4  * Authoer: Inki Dae <inki.dae@samsung.com>
5  *
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.
10  */
11 
12 #ifndef _EXYNOS_DRM_GEM_H_
13 #define _EXYNOS_DRM_GEM_H_
14 
15 #include <drm/drm_gem.h>
16 
17 #define to_exynos_gem_obj(x)	container_of(x,\
18 			struct exynos_drm_gem_obj, base)
19 
20 #define IS_NONCONTIG_BUFFER(f)		(f & EXYNOS_BO_NONCONTIG)
21 
22 /*
23  * exynos drm buffer structure.
24  *
25  * @base: a gem object.
26  *	- a new handle to this gem object would be created
27  *	by drm_gem_handle_create().
28  * @buffer: a pointer to exynos_drm_gem_buffer object.
29  *	- contain the information to memory region allocated
30  *	by user request or at framebuffer creation.
31  *	continuous memory region allocated by user request
32  *	or at framebuffer creation.
33  * @flags: indicate memory type to allocated buffer and cache attruibute.
34  * @size: size requested from user, in bytes and this size is aligned
35  *	in page unit.
36  * @cookie: cookie returned by dma_alloc_attrs
37  * @kvaddr: kernel virtual address to allocated memory region.
38  * @dma_addr: bus address(accessed by dma) to allocated memory region.
39  *	- this address could be physical address without IOMMU and
40  *	device address with IOMMU.
41  * @pages: Array of backing pages.
42  *
43  * P.S. this object would be transferred to user as kms_bo.handle so
44  *	user can access the buffer through kms_bo.handle.
45  */
46 struct exynos_drm_gem_obj {
47 	struct drm_gem_object	base;
48 	unsigned int		flags;
49 	unsigned long		size;
50 	void			*cookie;
51 	void __iomem		*kvaddr;
52 	dma_addr_t		dma_addr;
53 	struct dma_attrs	dma_attrs;
54 	struct page		**pages;
55 };
56 
57 struct page **exynos_gem_get_pages(struct drm_gem_object *obj, gfp_t gfpmask);
58 
59 /* destroy a buffer with gem object */
60 void exynos_drm_gem_destroy(struct exynos_drm_gem_obj *exynos_gem_obj);
61 
62 /* create a private gem object and initialize it. */
63 struct exynos_drm_gem_obj *exynos_drm_gem_init(struct drm_device *dev,
64 						      unsigned long size);
65 
66 /* create a new buffer with gem object */
67 struct exynos_drm_gem_obj *exynos_drm_gem_create(struct drm_device *dev,
68 						unsigned int flags,
69 						unsigned long size);
70 
71 /*
72  * request gem object creation and buffer allocation as the size
73  * that it is calculated with framebuffer information such as width,
74  * height and bpp.
75  */
76 int exynos_drm_gem_create_ioctl(struct drm_device *dev, void *data,
77 				struct drm_file *file_priv);
78 
79 /*
80  * get dma address from gem handle and this function could be used for
81  * other drivers such as 2d/3d acceleration drivers.
82  * with this function call, gem object reference count would be increased.
83  */
84 dma_addr_t *exynos_drm_gem_get_dma_addr(struct drm_device *dev,
85 					unsigned int gem_handle,
86 					struct drm_file *filp);
87 
88 /*
89  * put dma address from gem handle and this function could be used for
90  * other drivers such as 2d/3d acceleration drivers.
91  * with this function call, gem object reference count would be decreased.
92  */
93 void exynos_drm_gem_put_dma_addr(struct drm_device *dev,
94 					unsigned int gem_handle,
95 					struct drm_file *filp);
96 
97 /* map user space allocated by malloc to pages. */
98 int exynos_drm_gem_userptr_ioctl(struct drm_device *dev, void *data,
99 				      struct drm_file *file_priv);
100 
101 /* get buffer information to memory region allocated by gem. */
102 int exynos_drm_gem_get_ioctl(struct drm_device *dev, void *data,
103 				      struct drm_file *file_priv);
104 
105 /* get buffer size to gem handle. */
106 unsigned long exynos_drm_gem_get_size(struct drm_device *dev,
107 						unsigned int gem_handle,
108 						struct drm_file *file_priv);
109 
110 /* free gem object. */
111 void exynos_drm_gem_free_object(struct drm_gem_object *gem_obj);
112 
113 /* create memory region for drm framebuffer. */
114 int exynos_drm_gem_dumb_create(struct drm_file *file_priv,
115 			       struct drm_device *dev,
116 			       struct drm_mode_create_dumb *args);
117 
118 /* map memory region for drm framebuffer to user space. */
119 int exynos_drm_gem_dumb_map_offset(struct drm_file *file_priv,
120 				   struct drm_device *dev, uint32_t handle,
121 				   uint64_t *offset);
122 
123 /* page fault handler and mmap fault address(virtual) to physical memory. */
124 int exynos_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf);
125 
126 /* set vm_flags and we can change the vm attribute to other one at here. */
127 int exynos_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
128 
129 static inline int vma_is_io(struct vm_area_struct *vma)
130 {
131 	return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
132 }
133 
134 /* get a copy of a virtual memory region. */
135 struct vm_area_struct *exynos_gem_get_vma(struct vm_area_struct *vma);
136 
137 /* release a userspace virtual memory area. */
138 void exynos_gem_put_vma(struct vm_area_struct *vma);
139 
140 /* get pages from user space. */
141 int exynos_gem_get_pages_from_userptr(unsigned long start,
142 						unsigned int npages,
143 						struct page **pages,
144 						struct vm_area_struct *vma);
145 
146 /* drop the reference to pages. */
147 void exynos_gem_put_pages_to_userptr(struct page **pages,
148 					unsigned int npages,
149 					struct vm_area_struct *vma);
150 
151 /* map sgt with dma region. */
152 int exynos_gem_map_sgt_with_dma(struct drm_device *drm_dev,
153 				struct sg_table *sgt,
154 				enum dma_data_direction dir);
155 
156 /* unmap sgt from dma region. */
157 void exynos_gem_unmap_sgt_from_dma(struct drm_device *drm_dev,
158 				struct sg_table *sgt,
159 				enum dma_data_direction dir);
160 
161 /* low-level interface prime helpers */
162 struct sg_table *exynos_drm_gem_prime_get_sg_table(struct drm_gem_object *obj);
163 struct drm_gem_object *
164 exynos_drm_gem_prime_import_sg_table(struct drm_device *dev,
165 				     struct dma_buf_attachment *attach,
166 				     struct sg_table *sgt);
167 void *exynos_drm_gem_prime_vmap(struct drm_gem_object *obj);
168 void exynos_drm_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr);
169 
170 #endif
171