drm/ttm: merge ttm_backend and ttm_tt V5
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / gpu / drm / vmwgfx / vmwgfx_buffer.c
CommitLineData
fb1d9738
JB
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
32static uint32_t vram_placement_flags = TTM_PL_FLAG_VRAM |
33 TTM_PL_FLAG_CACHED;
34
35static uint32_t vram_ne_placement_flags = TTM_PL_FLAG_VRAM |
36 TTM_PL_FLAG_CACHED |
37 TTM_PL_FLAG_NO_EVICT;
38
39static uint32_t sys_placement_flags = TTM_PL_FLAG_SYSTEM |
40 TTM_PL_FLAG_CACHED;
41
135cba0d
TH
42static uint32_t gmr_placement_flags = VMW_PL_FLAG_GMR |
43 TTM_PL_FLAG_CACHED;
44
d991ef03
JB
45static uint32_t gmr_ne_placement_flags = VMW_PL_FLAG_GMR |
46 TTM_PL_FLAG_CACHED |
47 TTM_PL_FLAG_NO_EVICT;
48
fb1d9738
JB
49struct 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
135cba0d
TH
58static 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
5bb39e81
TH
63static 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
135cba0d
TH
68struct 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
d991ef03
JB
77static 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
82struct 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
8ba5152a
TH
91struct 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
fb1d9738
JB
100struct 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
109struct 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
d991ef03
JB
118static 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
124struct 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
5bb39e81
TH
133struct 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
649bf3ca
JG
142struct vmw_ttm_tt {
143 struct ttm_tt ttm;
135cba0d
TH
144 struct vmw_private *dev_priv;
145 int gmr_id;
fb1d9738
JB
146};
147
649bf3ca 148static int vmw_ttm_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
fb1d9738 149{
649bf3ca 150 struct vmw_ttm_tt *vmw_be = container_of(ttm, struct vmw_ttm_tt, ttm);
135cba0d
TH
151
152 vmw_be->gmr_id = bo_mem->start;
153
649bf3ca
JG
154 return vmw_gmr_bind(vmw_be->dev_priv, ttm->pages,
155 ttm->num_pages, vmw_be->gmr_id);
fb1d9738
JB
156}
157
649bf3ca 158static int vmw_ttm_unbind(struct ttm_tt *ttm)
fb1d9738 159{
649bf3ca 160 struct vmw_ttm_tt *vmw_be = container_of(ttm, struct vmw_ttm_tt, ttm);
135cba0d
TH
161
162 vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id);
fb1d9738
JB
163 return 0;
164}
165
649bf3ca 166static void vmw_ttm_destroy(struct ttm_tt *ttm)
fb1d9738 167{
649bf3ca 168 struct vmw_ttm_tt *vmw_be = container_of(ttm, struct vmw_ttm_tt, ttm);
fb1d9738
JB
169
170 kfree(vmw_be);
171}
172
173static struct ttm_backend_func vmw_ttm_func = {
fb1d9738
JB
174 .bind = vmw_ttm_bind,
175 .unbind = vmw_ttm_unbind,
176 .destroy = vmw_ttm_destroy,
177};
178
649bf3ca
JG
179struct 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)
fb1d9738 182{
649bf3ca 183 struct vmw_ttm_tt *vmw_be;
fb1d9738
JB
184
185 vmw_be = kmalloc(sizeof(*vmw_be), GFP_KERNEL);
186 if (!vmw_be)
187 return NULL;
188
649bf3ca 189 vmw_be->ttm.func = &vmw_ttm_func;
135cba0d 190 vmw_be->dev_priv = container_of(bdev, struct vmw_private, bdev);
fb1d9738 191
649bf3ca
JG
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;
fb1d9738
JB
197}
198
199int vmw_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
200{
201 return 0;
202}
203
204int vmw_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
205 struct ttm_mem_type_manager *man)
206{
fb1d9738
JB
207 switch (type) {
208 case TTM_PL_SYSTEM:
209 /* System memory */
210
211 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
135cba0d 212 man->available_caching = TTM_PL_FLAG_CACHED;
fb1d9738
JB
213 man->default_caching = TTM_PL_FLAG_CACHED;
214 break;
215 case TTM_PL_VRAM:
216 /* "On-card" video ram */
d961db75 217 man->func = &ttm_bo_manager_func;
fb1d9738 218 man->gpu_offset = 0;
96bf8b87 219 man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_MAPPABLE;
135cba0d
TH
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;
fb1d9738
JB
234 break;
235 default:
236 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
237 return -EINVAL;
238 }
239 return 0;
240}
241
242void 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
252static int vmw_verify_access(struct ttm_buffer_object *bo, struct file *filp)
253{
254 return 0;
255}
256
96bf8b87
JG
257static 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:
135cba0d 271 case VMW_PL_GMR:
96bf8b87
JG
272 return 0;
273 case TTM_PL_VRAM:
d961db75 274 mem->bus.offset = mem->start << PAGE_SHIFT;
96bf8b87
JG
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
284static void vmw_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
285{
286}
287
288static int vmw_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
289{
290 return 0;
291}
292
fb1d9738
JB
293/**
294 * FIXME: We're using the old vmware polling method to sync.
295 * Do this with fences instead.
296 */
297
298static void *vmw_sync_obj_ref(void *sync_obj)
299{
ae2a1040
TH
300
301 return (void *)
302 vmw_fence_obj_reference((struct vmw_fence_obj *) sync_obj);
fb1d9738
JB
303}
304
305static void vmw_sync_obj_unref(void **sync_obj)
306{
ae2a1040 307 vmw_fence_obj_unreference((struct vmw_fence_obj **) sync_obj);
fb1d9738
JB
308}
309
310static int vmw_sync_obj_flush(void *sync_obj, void *sync_arg)
311{
ae2a1040 312 vmw_fence_obj_flush((struct vmw_fence_obj *) sync_obj);
fb1d9738
JB
313 return 0;
314}
315
316static bool vmw_sync_obj_signaled(void *sync_obj, void *sync_arg)
317{
ae2a1040
TH
318 unsigned long flags = (unsigned long) sync_arg;
319 return vmw_fence_obj_signaled((struct vmw_fence_obj *) sync_obj,
320 (uint32_t) flags);
fb1d9738 321
fb1d9738
JB
322}
323
324static int vmw_sync_obj_wait(void *sync_obj, void *sync_arg,
325 bool lazy, bool interruptible)
326{
ae2a1040 327 unsigned long flags = (unsigned long) sync_arg;
fb1d9738 328
ae2a1040
TH
329 return vmw_fence_obj_wait((struct vmw_fence_obj *) sync_obj,
330 (uint32_t) flags,
331 lazy, interruptible,
332 VMW_FENCE_WAIT_TIMEOUT);
fb1d9738
JB
333}
334
335struct ttm_bo_driver vmw_bo_driver = {
649bf3ca 336 .ttm_tt_create = &vmw_ttm_tt_create,
fb1d9738
JB
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,
effe1105 346 .sync_obj_ref = vmw_sync_obj_ref,
135cba0d
TH
347 .move_notify = NULL,
348 .swap_notify = NULL,
96bf8b87
JG
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,
fb1d9738 352};