drm/sprd: save pid/tgid in private file data
[profile/mobile/platform/kernel/linux-3.10-sc7730.git] / drivers / gpu / drm / sprd / sprd_drm_gem.h
1 /* sprd_drm_gem.h
2  *
3  * Copyright (c) 2014 Spreadtrum Communications, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #ifndef _SPRD_DRM_GEM_H_
17 #define _SPRD_DRM_GEM_H_
18
19 #define to_sprd_gem_obj(x)      container_of(x,\
20                         struct sprd_drm_gem_obj, base)
21
22 #define IS_NONCONTIG_BUFFER(f)          (f & SPRD_BO_NONCONTIG)
23 #define IS_CACHABLE_BUFFER(f)           (f & SPRD_BO_CACHABLE)
24 #define IS_NONCACHABLE_BUFFER(f)        (f & SPRD_BO_NONCACHABLE)
25
26 #define IS_DEV_SYSTEM_BUFFER(f)                         (f & SPRD_BO_DEV_SYSTEM)
27 #define IS_DEV_MM_BUFFER(f)                             (f & SPRD_BO_DEV_MM)
28 #define IS_DEV_OVERLAY_BUFFER(f)                (f & SPRD_BO_DEV_OVERLAY)
29 #define IS_DEV_GSP_BUFFER(f)                    (f & SPRD_BO_DEV_GSP)
30
31 #define ENTRY_EMPTY -1
32 #define DRM_SPRD_HANDLE_WAIT_ENTRIES 10
33 #define SPRD_DRM_GEM_MAX_INDEX_ADDR 3
34
35 extern struct drm_device *sprd_drm_dev;
36
37 /*
38  * sprd drm gem buffer structure.
39  *
40  * @dma_addr: bus address(accessed by dma) to allocated memory region.
41  *      - this address could be physical address without IOMMU and
42  *      device address with IOMMU.
43  * @sgt: sg table to transfer page data.
44  * @pages: contain all pages to allocated memory region.
45  * @page_size: could be 4K, 64K or 1MB.
46  * @size: size of allocated memory region.
47  */
48 struct sprd_drm_gem_buf {
49         dma_addr_t              dma_addr;
50         struct sg_table         *sgt;
51         struct page             **pages;
52         unsigned long           page_size;
53         unsigned long           size;
54         struct ion_handle       *ion_handle;
55         bool                    pfnmap;
56         unsigned int            bufcount;
57         dma_addr_t              idx_addr[SPRD_DRM_GEM_MAX_INDEX_ADDR];
58
59         struct drm_gem_object *obj;
60 };
61
62 struct drm_sprd_gem_object_wait_list_entry {
63         struct list_head list;
64         int pid;
65         int in_use;
66         wait_queue_head_t process_wait_q;
67 };
68
69 /*
70  * sprd drm buffer structure.
71  *
72  * @base: a gem object.
73  *      - a new handle to this gem object would be created
74  *      by drm_gem_handle_create().
75  * @buffer: a pointer to sprd_drm_gem_buffer object.
76  *      - contain the information to memory region allocated
77  *      by user request or at framebuffer creation.
78  *      continuous memory region allocated by user request
79  *      or at framebuffer creation.
80  * @size: total memory size to physically non-continuous memory region.
81  * @flags: indicate memory type to allocated buffer and cache attruibute.
82  *
83  * P.S. this object would be transfered to user as kms_bo.handle so
84  *      user can access the buffer through kms_bo.handle.
85  */
86 struct sprd_drm_gem_obj {
87         struct drm_gem_object           base;
88         struct sprd_drm_gem_buf *buffer;
89         unsigned long                   size;
90         unsigned int                    flags;
91         int     lockpid;
92         pid_t   pid;
93         pid_t   tgid;
94         struct drm_sprd_gem_object_wait_list_entry
95         wait_entries[DRM_SPRD_HANDLE_WAIT_ENTRIES];
96
97         struct list_head wait_list;
98 };
99
100 struct page **sprd_gem_get_pages(struct drm_gem_object *obj, gfp_t gfpmask);
101
102 /* destroy a buffer with gem object */
103 void sprd_drm_gem_destroy(struct sprd_drm_gem_obj *sprd_gem_obj);
104
105 /* create a private gem object and initialize it. */
106 struct sprd_drm_gem_obj *sprd_drm_gem_init(struct drm_device *dev,
107                                                       unsigned long size);
108
109 /* create a new buffer with gem object */
110 struct sprd_drm_gem_obj *sprd_drm_gem_create(struct drm_device *dev,
111                                                 struct sprd_drm_gem_index *args);
112
113 /*
114  * request gem object creation and buffer allocation as the size
115  * that it is calculated with framebuffer information such as width,
116  * height and bpp.
117  */
118 int sprd_drm_gem_create_ioctl(struct drm_device *dev, void *data,
119                                 struct drm_file *file_priv);
120 int sprd_drm_gem_create_index_ioctl(struct drm_device *dev, void *data,
121                                 struct drm_file *file_priv);
122
123 struct dma_buf *sprd_prime_export(struct drm_device *dev,
124                                   struct drm_gem_object *obj, int flags);
125
126 struct drm_gem_object *sprd_prime_import(struct drm_device *dev,
127                                          struct dma_buf *dma_buf);
128
129 int sprd_drm_gem_prime_fd_to_handle(struct drm_device *dev,
130                 struct drm_file *file_priv, int prime_fd, uint32_t *handle);
131
132 /*
133  * get dma address from gem handle and this function could be used for
134  * other drivers such as 2d/3d acceleration drivers.
135  * with this function call, gem object reference count would be increased.
136  */
137 void *sprd_drm_gem_get_dma_addr(struct drm_device *dev,
138                                         unsigned int gem_handle,
139                                         struct drm_file *file_priv);
140
141 /*
142  * put dma address from gem handle and this function could be used for
143  * other drivers such as 2d/3d acceleration drivers.
144  * with this function call, gem object reference count would be decreased.
145  */
146 void sprd_drm_gem_put_dma_addr(struct drm_device *dev,
147                                         unsigned int gem_handle,
148                                         struct drm_file *file_priv);
149
150 unsigned long sprd_drm_gem_get_size(struct drm_device *dev,
151                                                 unsigned int gem_handle,
152                                                 struct drm_file *file_priv);
153
154 void *sprd_drm_gem_get_obj_addr(unsigned int name, unsigned int index);
155
156 /* get buffer offset to map to user space. */
157 int sprd_drm_gem_map_offset_ioctl(struct drm_device *dev, void *data,
158                                     struct drm_file *file_priv);
159
160 /*
161  * mmap the physically continuous memory that a gem object contains
162  * to user space.
163  */
164 int sprd_drm_gem_mmap_ioctl(struct drm_device *dev, void *data,
165                               struct drm_file *file_priv);
166
167 /* get buffer information to memory region allocated by gem. */
168 int sprd_drm_gem_get_ioctl(struct drm_device *dev, void *data,
169                                       struct drm_file *file_priv);
170
171 /* initialize gem object. */
172 int sprd_drm_gem_init_object(struct drm_gem_object *obj);
173
174 /* free gem object. */
175 void sprd_drm_gem_free_object(struct drm_gem_object *gem_obj);
176
177 /* create memory region for drm framebuffer. */
178 int sprd_drm_gem_dumb_create(struct drm_file *file_priv,
179                                struct drm_device *dev,
180                                struct drm_mode_create_dumb *args);
181
182 /* map memory region for drm framebuffer to user space. */
183 int sprd_drm_gem_dumb_map_offset(struct drm_file *file_priv,
184                                    struct drm_device *dev, uint32_t handle,
185                                    uint64_t *offset);
186
187 /*
188  * destroy memory region allocated.
189  *      - a gem handle and physical memory region pointed by a gem object
190  *      would be released by drm_gem_handle_delete().
191  */
192 int sprd_drm_gem_dumb_destroy(struct drm_file *file_priv,
193                                 struct drm_device *dev,
194                                 unsigned int handle);
195
196 /* page fault handler and mmap fault address(virtual) to physical memory. */
197 int sprd_drm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf);
198
199 /* set vm_flags and we can change the vm attribute to other one at here. */
200 int sprd_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
201
202 void init_fence(void);
203
204 int sprd_gem_lock_handle_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
205
206 int sprd_gem_unlock_handle_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
207
208 int sprd_gem_cache_op_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv);
209
210 int sprd_drm_gem_mmap_iommu_ioctl(struct drm_device *dev, void *data,
211                                 struct drm_file *file_priv);
212
213 int sprd_drm_gem_unmap_iommu_ioctl(struct drm_device *dev, void *data,
214                                 struct drm_file *file_priv);
215 #endif