]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/gpu/drm/vmwgfx/vmwgfx_buffer.c
vmwgfx: Make sure we always have a user-space handle to use for objects that are...
[mirror_ubuntu-artful-kernel.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
63struct 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
d991ef03
JB
72static 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
77struct 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
8ba5152a
TH
86struct 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
fb1d9738
JB
95struct 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
104struct 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
d991ef03
JB
113static 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
119struct 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
fb1d9738
JB
128struct vmw_ttm_backend {
129 struct ttm_backend backend;
135cba0d
TH
130 struct page **pages;
131 unsigned long num_pages;
132 struct vmw_private *dev_priv;
133 int gmr_id;
fb1d9738
JB
134};
135
136static int vmw_ttm_populate(struct ttm_backend *backend,
137 unsigned long num_pages, struct page **pages,
27e8b237
KRW
138 struct page *dummy_read_page,
139 dma_addr_t *dma_addrs)
fb1d9738 140{
135cba0d
TH
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
fb1d9738
JB
147 return 0;
148}
149
150static int vmw_ttm_bind(struct ttm_backend *backend, struct ttm_mem_reg *bo_mem)
151{
135cba0d
TH
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);
fb1d9738
JB
159}
160
161static int vmw_ttm_unbind(struct ttm_backend *backend)
162{
135cba0d
TH
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);
fb1d9738
JB
167 return 0;
168}
169
170static void vmw_ttm_clear(struct ttm_backend *backend)
171{
135cba0d
TH
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;
fb1d9738
JB
177}
178
179static 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
187static 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
195struct 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;
135cba0d 204 vmw_be->dev_priv = container_of(bdev, struct vmw_private, bdev);
fb1d9738
JB
205
206 return &vmw_be->backend;
207}
208
209int vmw_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
210{
211 return 0;
212}
213
214int vmw_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
215 struct ttm_mem_type_manager *man)
216{
fb1d9738
JB
217 switch (type) {
218 case TTM_PL_SYSTEM:
219 /* System memory */
220
221 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
135cba0d 222 man->available_caching = TTM_PL_FLAG_CACHED;
fb1d9738
JB
223 man->default_caching = TTM_PL_FLAG_CACHED;
224 break;
225 case TTM_PL_VRAM:
226 /* "On-card" video ram */
d961db75 227 man->func = &ttm_bo_manager_func;
fb1d9738 228 man->gpu_offset = 0;
96bf8b87 229 man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_MAPPABLE;
135cba0d
TH
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;
fb1d9738
JB
244 break;
245 default:
246 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
247 return -EINVAL;
248 }
249 return 0;
250}
251
252void 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
262static int vmw_verify_access(struct ttm_buffer_object *bo, struct file *filp)
263{
264 return 0;
265}
266
96bf8b87
JG
267static 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:
135cba0d 281 case VMW_PL_GMR:
96bf8b87
JG
282 return 0;
283 case TTM_PL_VRAM:
d961db75 284 mem->bus.offset = mem->start << PAGE_SHIFT;
96bf8b87
JG
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
294static void vmw_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
295{
296}
297
298static int vmw_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
299{
300 return 0;
301}
302
fb1d9738
JB
303/**
304 * FIXME: We're using the old vmware polling method to sync.
305 * Do this with fences instead.
306 */
307
308static void *vmw_sync_obj_ref(void *sync_obj)
309{
ae2a1040
TH
310
311 return (void *)
312 vmw_fence_obj_reference((struct vmw_fence_obj *) sync_obj);
fb1d9738
JB
313}
314
315static void vmw_sync_obj_unref(void **sync_obj)
316{
ae2a1040 317 vmw_fence_obj_unreference((struct vmw_fence_obj **) sync_obj);
fb1d9738
JB
318}
319
320static int vmw_sync_obj_flush(void *sync_obj, void *sync_arg)
321{
ae2a1040 322 vmw_fence_obj_flush((struct vmw_fence_obj *) sync_obj);
fb1d9738
JB
323 return 0;
324}
325
326static bool vmw_sync_obj_signaled(void *sync_obj, void *sync_arg)
327{
ae2a1040
TH
328 unsigned long flags = (unsigned long) sync_arg;
329 return vmw_fence_obj_signaled((struct vmw_fence_obj *) sync_obj,
330 (uint32_t) flags);
fb1d9738 331
fb1d9738
JB
332}
333
334static int vmw_sync_obj_wait(void *sync_obj, void *sync_arg,
335 bool lazy, bool interruptible)
336{
ae2a1040 337 unsigned long flags = (unsigned long) sync_arg;
fb1d9738 338
ae2a1040
TH
339 return vmw_fence_obj_wait((struct vmw_fence_obj *) sync_obj,
340 (uint32_t) flags,
341 lazy, interruptible,
342 VMW_FENCE_WAIT_TIMEOUT);
fb1d9738
JB
343}
344
345struct 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,
effe1105 356 .sync_obj_ref = vmw_sync_obj_ref,
135cba0d
TH
357 .move_notify = NULL,
358 .swap_notify = NULL,
96bf8b87
JG
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,
fb1d9738 362};