chromium: add drm drivers files based on
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / rockchip / rockchip_drm_gem.h
1 /*
2  * Copyright (C) ROCKCHIP, Inc.
3  * Author:yzq<yzq@rock-chips.com>
4  *
5  * based on exynos_drm_gem.h
6  *
7  * This software is licensed under the terms of the GNU General Public
8  * License version 2, as published by the Free Software Foundation, and
9  * may be copied, distributed, and modified under those terms.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16 #ifndef _ROCKCHIP_DRM_GEM_H_
17 #define _ROCKCHIP_DRM_GEM_H_
18
19 #define to_rockchip_gem_obj(x)  container_of(x,\
20                         struct rockchip_drm_gem_obj, base)
21
22 #define IS_NONCONTIG_BUFFER(f)          (f & ROCKCHIP_BO_NONCONTIG)
23
24 /*
25  * rockchip drm gem buffer structure.
26  *
27  * @kvaddr: kernel virtual address to allocated memory region.
28  * *userptr: user space address.
29  * @dma_addr: bus address(accessed by dma) to allocated memory region.
30  *      - this address could be physical address without IOMMU and
31  *      device address with IOMMU.
32  * @write: whether pages will be written to by the caller.
33  * @pages: Array of backing pages.
34  * @sgt: sg table to transfer page data.
35  * @size: size of allocated memory region.
36  * @pfnmap: indicate whether memory region from userptr is mmaped with
37  *      VM_PFNMAP or not.
38  */
39 struct rockchip_drm_gem_buf {
40         void __iomem            *kvaddr;
41         unsigned long           userptr;
42         dma_addr_t              dma_addr;
43         struct dma_attrs        dma_attrs;
44         unsigned int            write;
45         struct page             **pages;
46         struct sg_table         *sgt;
47         unsigned long           size;
48         bool                    pfnmap;
49 };
50
51 /*
52  * rockchip drm buffer structure.
53  *
54  * @base: a gem object.
55  *      - a new handle to this gem object would be created
56  *      by drm_gem_handle_create().
57  * @buffer: a pointer to rockchip_drm_gem_buffer object.
58  *      - contain the information to memory region allocated
59  *      by user request or at framebuffer creation.
60  *      continuous memory region allocated by user request
61  *      or at framebuffer creation.
62  * @size: size requested from user, in bytes and this size is aligned
63  *      in page unit.
64  * @vma: a pointer to vm_area.
65  * @flags: indicate memory type to allocated buffer and cache attruibute.
66  *
67  * P.S. this object would be transfered to user as kms_bo.handle so
68  *      user can access the buffer through kms_bo.handle.
69  */
70 struct rockchip_drm_gem_obj {
71         struct drm_gem_object           base;
72         struct rockchip_drm_gem_buf     *buffer;
73         unsigned long                   size;
74         struct vm_area_struct           *vma;
75         unsigned int                    flags;
76 };
77
78 struct page **rockchip_gem_get_pages(struct drm_gem_object *obj, gfp_t gfpmask);
79
80 /* destroy a buffer with gem object */
81 void rockchip_drm_gem_destroy(struct rockchip_drm_gem_obj *rockchip_gem_obj);
82
83 /* create a private gem object and initialize it. */
84 struct rockchip_drm_gem_obj *rockchip_drm_gem_init(struct drm_device *dev,
85                                                       unsigned long size);
86
87 /* create a new buffer with gem object */
88 struct rockchip_drm_gem_obj *rockchip_drm_gem_create(struct drm_device *dev,
89                                                 unsigned int flags,
90                                                 unsigned long size);
91
92 /*
93  * request gem object creation and buffer allocation as the size
94  * that it is calculated with framebuffer information such as width,
95  * height and bpp.
96  */
97 int rockchip_drm_gem_create_ioctl(struct drm_device *dev, void *data,
98                                 struct drm_file *file_priv);
99
100 /*
101  * get dma address from gem handle and this function could be used for
102  * other drivers such as 2d/3d acceleration drivers.
103  * with this function call, gem object reference count would be increased.
104  */
105 dma_addr_t *rockchip_drm_gem_get_dma_addr(struct drm_device *dev,
106                                         unsigned int gem_handle,
107                                         struct drm_file *filp);
108
109 /*
110  * put dma address from gem handle and this function could be used for
111  * other drivers such as 2d/3d acceleration drivers.
112  * with this function call, gem object reference count would be decreased.
113  */
114 void rockchip_drm_gem_put_dma_addr(struct drm_device *dev,
115                                         unsigned int gem_handle,
116                                         struct drm_file *filp);
117
118 /* get buffer offset to map to user space. */
119 int rockchip_drm_gem_map_offset_ioctl(struct drm_device *dev, void *data,
120                                     struct drm_file *file_priv);
121
122 /*
123  * mmap the physically continuous memory that a gem object contains
124  * to user space.
125  */
126 int rockchip_drm_gem_mmap_ioctl(struct drm_device *dev, void *data,
127                               struct drm_file *file_priv);
128
129 /* map user space allocated by malloc to pages. */
130 int rockchip_drm_gem_userptr_ioctl(struct drm_device *dev, void *data,
131                                       struct drm_file *file_priv);
132
133 /* get buffer information to memory region allocated by gem. */
134 int rockchip_drm_gem_get_ioctl(struct drm_device *dev, void *data,
135                                       struct drm_file *file_priv);
136
137 /* get buffer size to gem handle. */
138 unsigned long rockchip_drm_gem_get_size(struct drm_device *dev,
139                                                 unsigned int gem_handle,
140                                                 struct drm_file *file_priv);
141
142 /* initialize gem object. */
143 int rockchip_drm_gem_init_object(struct drm_gem_object *obj);
144
145 /* free gem object. */
146 void rockchip_drm_gem_free_object(struct drm_gem_object *gem_obj);
147
148 /* create memory region for drm framebuffer. */
149 int rockchip_drm_gem_dumb_create(struct drm_file *file_priv,
150                                struct drm_device *dev,
151                                struct drm_mode_create_dumb *args);
152
153 /* map memory region for drm framebuffer to user space. */
154 int rockchip_drm_gem_dumb_map_offset(struct drm_file *file_priv,
155                                    struct drm_device *dev, uint32_t handle,
156                                    uint64_t *offset);
157
158 /*
159  * destroy memory region allocated.
160  *      - a gem handle and physical memory region pointed by a gem object
161  *      would be released by drm_gem_handle_delete().
162  */
163 int rockchip_drm_gem_dumb_destroy(struct drm_file *file_priv,
164                                 struct drm_device *dev,
165                                 unsigned int handle);
166
167 /* page fault handler and mmap fault address(virtual) to physical memory. */
168 int rockchip_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf);
169
170 /* set vm_flags and we can change the vm attribute to other one at here. */
171 int rockchip_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
172
173 static inline int vma_is_io(struct vm_area_struct *vma)
174 {
175         return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
176 }
177
178 /* get a copy of a virtual memory region. */
179 struct vm_area_struct *rockchip_gem_get_vma(struct vm_area_struct *vma);
180
181 /* release a userspace virtual memory area. */
182 void rockchip_gem_put_vma(struct vm_area_struct *vma);
183
184 /* get pages from user space. */
185 int rockchip_gem_get_pages_from_userptr(unsigned long start,
186                                                 unsigned int npages,
187                                                 struct page **pages,
188                                                 struct vm_area_struct *vma);
189
190 /* drop the reference to pages. */
191 void rockchip_gem_put_pages_to_userptr(struct page **pages,
192                                         unsigned int npages,
193                                         struct vm_area_struct *vma);
194
195 /* map sgt with dma region. */
196 int rockchip_gem_map_sgt_with_dma(struct drm_device *drm_dev,
197                                 struct sg_table *sgt,
198                                 enum dma_data_direction dir);
199
200 /* unmap sgt from dma region. */
201 void rockchip_gem_unmap_sgt_from_dma(struct drm_device *drm_dev,
202                                 struct sg_table *sgt,
203                                 enum dma_data_direction dir);
204
205 #endif