drm/ttm: merge ttm_backend and ttm_tt V5
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / gpu / drm / vmwgfx / vmwgfx_buffer.c
1 /**************************************************************************
2 *
3 * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "vmwgfx_drv.h"
29 #include "ttm/ttm_bo_driver.h"
30 #include "ttm/ttm_placement.h"
31
32 static uint32_t vram_placement_flags = TTM_PL_FLAG_VRAM |
33 TTM_PL_FLAG_CACHED;
34
35 static uint32_t vram_ne_placement_flags = TTM_PL_FLAG_VRAM |
36 TTM_PL_FLAG_CACHED |
37 TTM_PL_FLAG_NO_EVICT;
38
39 static uint32_t sys_placement_flags = TTM_PL_FLAG_SYSTEM |
40 TTM_PL_FLAG_CACHED;
41
42 static uint32_t gmr_placement_flags = VMW_PL_FLAG_GMR |
43 TTM_PL_FLAG_CACHED;
44
45 static uint32_t gmr_ne_placement_flags = VMW_PL_FLAG_GMR |
46 TTM_PL_FLAG_CACHED |
47 TTM_PL_FLAG_NO_EVICT;
48
49 struct ttm_placement vmw_vram_placement = {
50 .fpfn = 0,
51 .lpfn = 0,
52 .num_placement = 1,
53 .placement = &vram_placement_flags,
54 .num_busy_placement = 1,
55 .busy_placement = &vram_placement_flags
56 };
57
58 static uint32_t vram_gmr_placement_flags[] = {
59 TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED,
60 VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED
61 };
62
63 static uint32_t gmr_vram_placement_flags[] = {
64 VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED,
65 TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED
66 };
67
68 struct ttm_placement vmw_vram_gmr_placement = {
69 .fpfn = 0,
70 .lpfn = 0,
71 .num_placement = 2,
72 .placement = vram_gmr_placement_flags,
73 .num_busy_placement = 1,
74 .busy_placement = &gmr_placement_flags
75 };
76
77 static uint32_t vram_gmr_ne_placement_flags[] = {
78 TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT,
79 VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT
80 };
81
82 struct ttm_placement vmw_vram_gmr_ne_placement = {
83 .fpfn = 0,
84 .lpfn = 0,
85 .num_placement = 2,
86 .placement = vram_gmr_ne_placement_flags,
87 .num_busy_placement = 1,
88 .busy_placement = &gmr_ne_placement_flags
89 };
90
91 struct ttm_placement vmw_vram_sys_placement = {
92 .fpfn = 0,
93 .lpfn = 0,
94 .num_placement = 1,
95 .placement = &vram_placement_flags,
96 .num_busy_placement = 1,
97 .busy_placement = &sys_placement_flags
98 };
99
100 struct ttm_placement vmw_vram_ne_placement = {
101 .fpfn = 0,
102 .lpfn = 0,
103 .num_placement = 1,
104 .placement = &vram_ne_placement_flags,
105 .num_busy_placement = 1,
106 .busy_placement = &vram_ne_placement_flags
107 };
108
109 struct ttm_placement vmw_sys_placement = {
110 .fpfn = 0,
111 .lpfn = 0,
112 .num_placement = 1,
113 .placement = &sys_placement_flags,
114 .num_busy_placement = 1,
115 .busy_placement = &sys_placement_flags
116 };
117
118 static uint32_t evictable_placement_flags[] = {
119 TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED,
120 TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED,
121 VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED
122 };
123
124 struct ttm_placement vmw_evictable_placement = {
125 .fpfn = 0,
126 .lpfn = 0,
127 .num_placement = 3,
128 .placement = evictable_placement_flags,
129 .num_busy_placement = 1,
130 .busy_placement = &sys_placement_flags
131 };
132
133 struct ttm_placement vmw_srf_placement = {
134 .fpfn = 0,
135 .lpfn = 0,
136 .num_placement = 1,
137 .num_busy_placement = 2,
138 .placement = &gmr_placement_flags,
139 .busy_placement = gmr_vram_placement_flags
140 };
141
142 struct vmw_ttm_tt {
143 struct ttm_tt ttm;
144 struct vmw_private *dev_priv;
145 int gmr_id;
146 };
147
148 static int vmw_ttm_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
149 {
150 struct vmw_ttm_tt *vmw_be = container_of(ttm, struct vmw_ttm_tt, ttm);
151
152 vmw_be->gmr_id = bo_mem->start;
153
154 return vmw_gmr_bind(vmw_be->dev_priv, ttm->pages,
155 ttm->num_pages, vmw_be->gmr_id);
156 }
157
158 static int vmw_ttm_unbind(struct ttm_tt *ttm)
159 {
160 struct vmw_ttm_tt *vmw_be = container_of(ttm, struct vmw_ttm_tt, ttm);
161
162 vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id);
163 return 0;
164 }
165
166 static void vmw_ttm_destroy(struct ttm_tt *ttm)
167 {
168 struct vmw_ttm_tt *vmw_be = container_of(ttm, struct vmw_ttm_tt, ttm);
169
170 kfree(vmw_be);
171 }
172
173 static struct ttm_backend_func vmw_ttm_func = {
174 .bind = vmw_ttm_bind,
175 .unbind = vmw_ttm_unbind,
176 .destroy = vmw_ttm_destroy,
177 };
178
179 struct ttm_tt *vmw_ttm_tt_create(struct ttm_bo_device *bdev,
180 unsigned long size, uint32_t page_flags,
181 struct page *dummy_read_page)
182 {
183 struct vmw_ttm_tt *vmw_be;
184
185 vmw_be = kmalloc(sizeof(*vmw_be), GFP_KERNEL);
186 if (!vmw_be)
187 return NULL;
188
189 vmw_be->ttm.func = &vmw_ttm_func;
190 vmw_be->dev_priv = container_of(bdev, struct vmw_private, bdev);
191
192 if (ttm_tt_init(&vmw_be->ttm, bdev, size, page_flags, dummy_read_page)) {
193 return NULL;
194 }
195
196 return &vmw_be->ttm;
197 }
198
199 int vmw_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
200 {
201 return 0;
202 }
203
204 int vmw_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
205 struct ttm_mem_type_manager *man)
206 {
207 switch (type) {
208 case TTM_PL_SYSTEM:
209 /* System memory */
210
211 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
212 man->available_caching = TTM_PL_FLAG_CACHED;
213 man->default_caching = TTM_PL_FLAG_CACHED;
214 break;
215 case TTM_PL_VRAM:
216 /* "On-card" video ram */
217 man->func = &ttm_bo_manager_func;
218 man->gpu_offset = 0;
219 man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_MAPPABLE;
220 man->available_caching = TTM_PL_FLAG_CACHED;
221 man->default_caching = TTM_PL_FLAG_CACHED;
222 break;
223 case VMW_PL_GMR:
224 /*
225 * "Guest Memory Regions" is an aperture like feature with
226 * one slot per bo. There is an upper limit of the number of
227 * slots as well as the bo size.
228 */
229 man->func = &vmw_gmrid_manager_func;
230 man->gpu_offset = 0;
231 man->flags = TTM_MEMTYPE_FLAG_CMA | TTM_MEMTYPE_FLAG_MAPPABLE;
232 man->available_caching = TTM_PL_FLAG_CACHED;
233 man->default_caching = TTM_PL_FLAG_CACHED;
234 break;
235 default:
236 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
237 return -EINVAL;
238 }
239 return 0;
240 }
241
242 void vmw_evict_flags(struct ttm_buffer_object *bo,
243 struct ttm_placement *placement)
244 {
245 *placement = vmw_sys_placement;
246 }
247
248 /**
249 * FIXME: Proper access checks on buffers.
250 */
251
252 static int vmw_verify_access(struct ttm_buffer_object *bo, struct file *filp)
253 {
254 return 0;
255 }
256
257 static int vmw_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
258 {
259 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
260 struct vmw_private *dev_priv = container_of(bdev, struct vmw_private, bdev);
261
262 mem->bus.addr = NULL;
263 mem->bus.is_iomem = false;
264 mem->bus.offset = 0;
265 mem->bus.size = mem->num_pages << PAGE_SHIFT;
266 mem->bus.base = 0;
267 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
268 return -EINVAL;
269 switch (mem->mem_type) {
270 case TTM_PL_SYSTEM:
271 case VMW_PL_GMR:
272 return 0;
273 case TTM_PL_VRAM:
274 mem->bus.offset = mem->start << PAGE_SHIFT;
275 mem->bus.base = dev_priv->vram_start;
276 mem->bus.is_iomem = true;
277 break;
278 default:
279 return -EINVAL;
280 }
281 return 0;
282 }
283
284 static void vmw_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
285 {
286 }
287
288 static int vmw_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
289 {
290 return 0;
291 }
292
293 /**
294 * FIXME: We're using the old vmware polling method to sync.
295 * Do this with fences instead.
296 */
297
298 static void *vmw_sync_obj_ref(void *sync_obj)
299 {
300
301 return (void *)
302 vmw_fence_obj_reference((struct vmw_fence_obj *) sync_obj);
303 }
304
305 static void vmw_sync_obj_unref(void **sync_obj)
306 {
307 vmw_fence_obj_unreference((struct vmw_fence_obj **) sync_obj);
308 }
309
310 static int vmw_sync_obj_flush(void *sync_obj, void *sync_arg)
311 {
312 vmw_fence_obj_flush((struct vmw_fence_obj *) sync_obj);
313 return 0;
314 }
315
316 static bool vmw_sync_obj_signaled(void *sync_obj, void *sync_arg)
317 {
318 unsigned long flags = (unsigned long) sync_arg;
319 return vmw_fence_obj_signaled((struct vmw_fence_obj *) sync_obj,
320 (uint32_t) flags);
321
322 }
323
324 static int vmw_sync_obj_wait(void *sync_obj, void *sync_arg,
325 bool lazy, bool interruptible)
326 {
327 unsigned long flags = (unsigned long) sync_arg;
328
329 return vmw_fence_obj_wait((struct vmw_fence_obj *) sync_obj,
330 (uint32_t) flags,
331 lazy, interruptible,
332 VMW_FENCE_WAIT_TIMEOUT);
333 }
334
335 struct ttm_bo_driver vmw_bo_driver = {
336 .ttm_tt_create = &vmw_ttm_tt_create,
337 .invalidate_caches = vmw_invalidate_caches,
338 .init_mem_type = vmw_init_mem_type,
339 .evict_flags = vmw_evict_flags,
340 .move = NULL,
341 .verify_access = vmw_verify_access,
342 .sync_obj_signaled = vmw_sync_obj_signaled,
343 .sync_obj_wait = vmw_sync_obj_wait,
344 .sync_obj_flush = vmw_sync_obj_flush,
345 .sync_obj_unref = vmw_sync_obj_unref,
346 .sync_obj_ref = vmw_sync_obj_ref,
347 .move_notify = NULL,
348 .swap_notify = NULL,
349 .fault_reserve_notify = &vmw_ttm_fault_reserve_notify,
350 .io_mem_reserve = &vmw_ttm_io_mem_reserve,
351 .io_mem_free = &vmw_ttm_io_mem_free,
352 };