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