vmwgfx: Add dmabuf helper functions for pinning
[pandora-kernel.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 struct ttm_placement vmw_vram_gmr_placement = {
64         .fpfn = 0,
65         .lpfn = 0,
66         .num_placement = 2,
67         .placement = vram_gmr_placement_flags,
68         .num_busy_placement = 1,
69         .busy_placement = &gmr_placement_flags
70 };
71
72 static uint32_t vram_gmr_ne_placement_flags[] = {
73         TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT,
74         VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED | TTM_PL_FLAG_NO_EVICT
75 };
76
77 struct ttm_placement vmw_vram_gmr_ne_placement = {
78         .fpfn = 0,
79         .lpfn = 0,
80         .num_placement = 2,
81         .placement = vram_gmr_ne_placement_flags,
82         .num_busy_placement = 1,
83         .busy_placement = &gmr_ne_placement_flags
84 };
85
86 struct ttm_placement vmw_vram_sys_placement = {
87         .fpfn = 0,
88         .lpfn = 0,
89         .num_placement = 1,
90         .placement = &vram_placement_flags,
91         .num_busy_placement = 1,
92         .busy_placement = &sys_placement_flags
93 };
94
95 struct ttm_placement vmw_vram_ne_placement = {
96         .fpfn = 0,
97         .lpfn = 0,
98         .num_placement = 1,
99         .placement = &vram_ne_placement_flags,
100         .num_busy_placement = 1,
101         .busy_placement = &vram_ne_placement_flags
102 };
103
104 struct ttm_placement vmw_sys_placement = {
105         .fpfn = 0,
106         .lpfn = 0,
107         .num_placement = 1,
108         .placement = &sys_placement_flags,
109         .num_busy_placement = 1,
110         .busy_placement = &sys_placement_flags
111 };
112
113 static uint32_t evictable_placement_flags[] = {
114         TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED,
115         TTM_PL_FLAG_VRAM | TTM_PL_FLAG_CACHED,
116         VMW_PL_FLAG_GMR | TTM_PL_FLAG_CACHED
117 };
118
119 struct ttm_placement vmw_evictable_placement = {
120         .fpfn = 0,
121         .lpfn = 0,
122         .num_placement = 3,
123         .placement = evictable_placement_flags,
124         .num_busy_placement = 1,
125         .busy_placement = &sys_placement_flags
126 };
127
128 struct vmw_ttm_backend {
129         struct ttm_backend backend;
130         struct page **pages;
131         unsigned long num_pages;
132         struct vmw_private *dev_priv;
133         int gmr_id;
134 };
135
136 static int vmw_ttm_populate(struct ttm_backend *backend,
137                             unsigned long num_pages, struct page **pages,
138                             struct page *dummy_read_page,
139                             dma_addr_t *dma_addrs)
140 {
141         struct vmw_ttm_backend *vmw_be =
142             container_of(backend, struct vmw_ttm_backend, backend);
143
144         vmw_be->pages = pages;
145         vmw_be->num_pages = num_pages;
146
147         return 0;
148 }
149
150 static int vmw_ttm_bind(struct ttm_backend *backend, struct ttm_mem_reg *bo_mem)
151 {
152         struct vmw_ttm_backend *vmw_be =
153             container_of(backend, struct vmw_ttm_backend, backend);
154
155         vmw_be->gmr_id = bo_mem->start;
156
157         return vmw_gmr_bind(vmw_be->dev_priv, vmw_be->pages,
158                             vmw_be->num_pages, vmw_be->gmr_id);
159 }
160
161 static int vmw_ttm_unbind(struct ttm_backend *backend)
162 {
163         struct vmw_ttm_backend *vmw_be =
164             container_of(backend, struct vmw_ttm_backend, backend);
165
166         vmw_gmr_unbind(vmw_be->dev_priv, vmw_be->gmr_id);
167         return 0;
168 }
169
170 static void vmw_ttm_clear(struct ttm_backend *backend)
171 {
172         struct vmw_ttm_backend *vmw_be =
173                 container_of(backend, struct vmw_ttm_backend, backend);
174
175         vmw_be->pages = NULL;
176         vmw_be->num_pages = 0;
177 }
178
179 static void vmw_ttm_destroy(struct ttm_backend *backend)
180 {
181         struct vmw_ttm_backend *vmw_be =
182             container_of(backend, struct vmw_ttm_backend, backend);
183
184         kfree(vmw_be);
185 }
186
187 static struct ttm_backend_func vmw_ttm_func = {
188         .populate = vmw_ttm_populate,
189         .clear = vmw_ttm_clear,
190         .bind = vmw_ttm_bind,
191         .unbind = vmw_ttm_unbind,
192         .destroy = vmw_ttm_destroy,
193 };
194
195 struct ttm_backend *vmw_ttm_backend_init(struct ttm_bo_device *bdev)
196 {
197         struct vmw_ttm_backend *vmw_be;
198
199         vmw_be = kmalloc(sizeof(*vmw_be), GFP_KERNEL);
200         if (!vmw_be)
201                 return NULL;
202
203         vmw_be->backend.func = &vmw_ttm_func;
204         vmw_be->dev_priv = container_of(bdev, struct vmw_private, bdev);
205
206         return &vmw_be->backend;
207 }
208
209 int vmw_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
210 {
211         return 0;
212 }
213
214 int vmw_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
215                       struct ttm_mem_type_manager *man)
216 {
217         switch (type) {
218         case TTM_PL_SYSTEM:
219                 /* System memory */
220
221                 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
222                 man->available_caching = TTM_PL_FLAG_CACHED;
223                 man->default_caching = TTM_PL_FLAG_CACHED;
224                 break;
225         case TTM_PL_VRAM:
226                 /* "On-card" video ram */
227                 man->func = &ttm_bo_manager_func;
228                 man->gpu_offset = 0;
229                 man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_MAPPABLE;
230                 man->available_caching = TTM_PL_FLAG_CACHED;
231                 man->default_caching = TTM_PL_FLAG_CACHED;
232                 break;
233         case VMW_PL_GMR:
234                 /*
235                  * "Guest Memory Regions" is an aperture like feature with
236                  *  one slot per bo. There is an upper limit of the number of
237                  *  slots as well as the bo size.
238                  */
239                 man->func = &vmw_gmrid_manager_func;
240                 man->gpu_offset = 0;
241                 man->flags = TTM_MEMTYPE_FLAG_CMA | TTM_MEMTYPE_FLAG_MAPPABLE;
242                 man->available_caching = TTM_PL_FLAG_CACHED;
243                 man->default_caching = TTM_PL_FLAG_CACHED;
244                 break;
245         default:
246                 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
247                 return -EINVAL;
248         }
249         return 0;
250 }
251
252 void vmw_evict_flags(struct ttm_buffer_object *bo,
253                      struct ttm_placement *placement)
254 {
255         *placement = vmw_sys_placement;
256 }
257
258 /**
259  * FIXME: Proper access checks on buffers.
260  */
261
262 static int vmw_verify_access(struct ttm_buffer_object *bo, struct file *filp)
263 {
264         return 0;
265 }
266
267 static int vmw_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
268 {
269         struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
270         struct vmw_private *dev_priv = container_of(bdev, struct vmw_private, bdev);
271
272         mem->bus.addr = NULL;
273         mem->bus.is_iomem = false;
274         mem->bus.offset = 0;
275         mem->bus.size = mem->num_pages << PAGE_SHIFT;
276         mem->bus.base = 0;
277         if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
278                 return -EINVAL;
279         switch (mem->mem_type) {
280         case TTM_PL_SYSTEM:
281         case VMW_PL_GMR:
282                 return 0;
283         case TTM_PL_VRAM:
284                 mem->bus.offset = mem->start << PAGE_SHIFT;
285                 mem->bus.base = dev_priv->vram_start;
286                 mem->bus.is_iomem = true;
287                 break;
288         default:
289                 return -EINVAL;
290         }
291         return 0;
292 }
293
294 static void vmw_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
295 {
296 }
297
298 static int vmw_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
299 {
300         return 0;
301 }
302
303 /**
304  * FIXME: We're using the old vmware polling method to sync.
305  * Do this with fences instead.
306  */
307
308 static void *vmw_sync_obj_ref(void *sync_obj)
309 {
310
311         return (void *)
312                 vmw_fence_obj_reference((struct vmw_fence_obj *) sync_obj);
313 }
314
315 static void vmw_sync_obj_unref(void **sync_obj)
316 {
317         vmw_fence_obj_unreference((struct vmw_fence_obj **) sync_obj);
318 }
319
320 static int vmw_sync_obj_flush(void *sync_obj, void *sync_arg)
321 {
322         vmw_fence_obj_flush((struct vmw_fence_obj *) sync_obj);
323         return 0;
324 }
325
326 static bool vmw_sync_obj_signaled(void *sync_obj, void *sync_arg)
327 {
328         unsigned long flags = (unsigned long) sync_arg;
329         return  vmw_fence_obj_signaled((struct vmw_fence_obj *) sync_obj,
330                                        (uint32_t) flags);
331
332 }
333
334 static int vmw_sync_obj_wait(void *sync_obj, void *sync_arg,
335                              bool lazy, bool interruptible)
336 {
337         unsigned long flags = (unsigned long) sync_arg;
338
339         return vmw_fence_obj_wait((struct vmw_fence_obj *) sync_obj,
340                                   (uint32_t) flags,
341                                   lazy, interruptible,
342                                   VMW_FENCE_WAIT_TIMEOUT);
343 }
344
345 struct ttm_bo_driver vmw_bo_driver = {
346         .create_ttm_backend_entry = vmw_ttm_backend_init,
347         .invalidate_caches = vmw_invalidate_caches,
348         .init_mem_type = vmw_init_mem_type,
349         .evict_flags = vmw_evict_flags,
350         .move = NULL,
351         .verify_access = vmw_verify_access,
352         .sync_obj_signaled = vmw_sync_obj_signaled,
353         .sync_obj_wait = vmw_sync_obj_wait,
354         .sync_obj_flush = vmw_sync_obj_flush,
355         .sync_obj_unref = vmw_sync_obj_unref,
356         .sync_obj_ref = vmw_sync_obj_ref,
357         .move_notify = NULL,
358         .swap_notify = NULL,
359         .fault_reserve_notify = &vmw_ttm_fault_reserve_notify,
360         .io_mem_reserve = &vmw_ttm_io_mem_reserve,
361         .io_mem_free = &vmw_ttm_io_mem_free,
362 };