blob: 1b3bd8c6c67e007a4e9e079881c6101cc9e994ca [file] [log] [blame]
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001/**************************************************************************
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
42struct ttm_placement vmw_vram_placement = {
43 .fpfn = 0,
44 .lpfn = 0,
45 .num_placement = 1,
46 .placement = &vram_placement_flags,
47 .num_busy_placement = 1,
48 .busy_placement = &vram_placement_flags
49};
50
Thomas Hellstrom8ba51522010-01-16 16:05:05 +010051struct ttm_placement vmw_vram_sys_placement = {
52 .fpfn = 0,
53 .lpfn = 0,
54 .num_placement = 1,
55 .placement = &vram_placement_flags,
56 .num_busy_placement = 1,
57 .busy_placement = &sys_placement_flags
58};
59
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000060struct ttm_placement vmw_vram_ne_placement = {
61 .fpfn = 0,
62 .lpfn = 0,
63 .num_placement = 1,
64 .placement = &vram_ne_placement_flags,
65 .num_busy_placement = 1,
66 .busy_placement = &vram_ne_placement_flags
67};
68
69struct ttm_placement vmw_sys_placement = {
70 .fpfn = 0,
71 .lpfn = 0,
72 .num_placement = 1,
73 .placement = &sys_placement_flags,
74 .num_busy_placement = 1,
75 .busy_placement = &sys_placement_flags
76};
77
78struct vmw_ttm_backend {
79 struct ttm_backend backend;
80};
81
82static int vmw_ttm_populate(struct ttm_backend *backend,
83 unsigned long num_pages, struct page **pages,
84 struct page *dummy_read_page)
85{
86 return 0;
87}
88
89static int vmw_ttm_bind(struct ttm_backend *backend, struct ttm_mem_reg *bo_mem)
90{
91 return 0;
92}
93
94static int vmw_ttm_unbind(struct ttm_backend *backend)
95{
96 return 0;
97}
98
99static void vmw_ttm_clear(struct ttm_backend *backend)
100{
101}
102
103static void vmw_ttm_destroy(struct ttm_backend *backend)
104{
105 struct vmw_ttm_backend *vmw_be =
106 container_of(backend, struct vmw_ttm_backend, backend);
107
108 kfree(vmw_be);
109}
110
111static struct ttm_backend_func vmw_ttm_func = {
112 .populate = vmw_ttm_populate,
113 .clear = vmw_ttm_clear,
114 .bind = vmw_ttm_bind,
115 .unbind = vmw_ttm_unbind,
116 .destroy = vmw_ttm_destroy,
117};
118
119struct ttm_backend *vmw_ttm_backend_init(struct ttm_bo_device *bdev)
120{
121 struct vmw_ttm_backend *vmw_be;
122
123 vmw_be = kmalloc(sizeof(*vmw_be), GFP_KERNEL);
124 if (!vmw_be)
125 return NULL;
126
127 vmw_be->backend.func = &vmw_ttm_func;
128
129 return &vmw_be->backend;
130}
131
132int vmw_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
133{
134 return 0;
135}
136
137int vmw_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
138 struct ttm_mem_type_manager *man)
139{
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000140 switch (type) {
141 case TTM_PL_SYSTEM:
142 /* System memory */
143
144 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
145 man->available_caching = TTM_PL_MASK_CACHING;
146 man->default_caching = TTM_PL_FLAG_CACHED;
147 break;
148 case TTM_PL_VRAM:
149 /* "On-card" video ram */
Ben Skeggsd961db72010-08-05 10:48:18 +1000150 man->func = &ttm_bo_manager_func;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000151 man->gpu_offset = 0;
Jerome Glisse96bf8b82010-04-09 14:39:26 +0200152 man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_MAPPABLE;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000153 man->available_caching = TTM_PL_MASK_CACHING;
154 man->default_caching = TTM_PL_FLAG_WC;
155 break;
156 default:
157 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
158 return -EINVAL;
159 }
160 return 0;
161}
162
163void vmw_evict_flags(struct ttm_buffer_object *bo,
164 struct ttm_placement *placement)
165{
166 *placement = vmw_sys_placement;
167}
168
169/**
170 * FIXME: Proper access checks on buffers.
171 */
172
173static int vmw_verify_access(struct ttm_buffer_object *bo, struct file *filp)
174{
175 return 0;
176}
177
Thomas Hellstromeffe1102010-01-13 22:28:39 +0100178static void vmw_move_notify(struct ttm_buffer_object *bo,
179 struct ttm_mem_reg *new_mem)
180{
181 if (new_mem->mem_type != TTM_PL_SYSTEM)
182 vmw_dmabuf_gmr_unbind(bo);
183}
184
Thomas Hellstrom476d51d2010-01-13 22:28:41 +0100185static void vmw_swap_notify(struct ttm_buffer_object *bo)
186{
187 vmw_dmabuf_gmr_unbind(bo);
188}
189
Jerome Glisse96bf8b82010-04-09 14:39:26 +0200190static int vmw_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
191{
192 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
193 struct vmw_private *dev_priv = container_of(bdev, struct vmw_private, bdev);
194
195 mem->bus.addr = NULL;
196 mem->bus.is_iomem = false;
197 mem->bus.offset = 0;
198 mem->bus.size = mem->num_pages << PAGE_SHIFT;
199 mem->bus.base = 0;
200 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
201 return -EINVAL;
202 switch (mem->mem_type) {
203 case TTM_PL_SYSTEM:
204 /* System memory */
205 return 0;
206 case TTM_PL_VRAM:
Ben Skeggsd961db72010-08-05 10:48:18 +1000207 mem->bus.offset = mem->start << PAGE_SHIFT;
Jerome Glisse96bf8b82010-04-09 14:39:26 +0200208 mem->bus.base = dev_priv->vram_start;
209 mem->bus.is_iomem = true;
210 break;
211 default:
212 return -EINVAL;
213 }
214 return 0;
215}
216
217static void vmw_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
218{
219}
220
221static int vmw_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
222{
223 return 0;
224}
225
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000226/**
227 * FIXME: We're using the old vmware polling method to sync.
228 * Do this with fences instead.
229 */
230
231static void *vmw_sync_obj_ref(void *sync_obj)
232{
233 return sync_obj;
234}
235
236static void vmw_sync_obj_unref(void **sync_obj)
237{
238 *sync_obj = NULL;
239}
240
241static int vmw_sync_obj_flush(void *sync_obj, void *sync_arg)
242{
243 struct vmw_private *dev_priv = (struct vmw_private *)sync_arg;
244
245 mutex_lock(&dev_priv->hw_mutex);
246 vmw_write(dev_priv, SVGA_REG_SYNC, SVGA_SYNC_GENERIC);
247 mutex_unlock(&dev_priv->hw_mutex);
248 return 0;
249}
250
251static bool vmw_sync_obj_signaled(void *sync_obj, void *sync_arg)
252{
253 struct vmw_private *dev_priv = (struct vmw_private *)sync_arg;
254 uint32_t sequence = (unsigned long) sync_obj;
255
256 return vmw_fence_signaled(dev_priv, sequence);
257}
258
259static int vmw_sync_obj_wait(void *sync_obj, void *sync_arg,
260 bool lazy, bool interruptible)
261{
262 struct vmw_private *dev_priv = (struct vmw_private *)sync_arg;
263 uint32_t sequence = (unsigned long) sync_obj;
264
265 return vmw_wait_fence(dev_priv, false, sequence, false, 3*HZ);
266}
267
268struct ttm_bo_driver vmw_bo_driver = {
269 .create_ttm_backend_entry = vmw_ttm_backend_init,
270 .invalidate_caches = vmw_invalidate_caches,
271 .init_mem_type = vmw_init_mem_type,
272 .evict_flags = vmw_evict_flags,
273 .move = NULL,
274 .verify_access = vmw_verify_access,
275 .sync_obj_signaled = vmw_sync_obj_signaled,
276 .sync_obj_wait = vmw_sync_obj_wait,
277 .sync_obj_flush = vmw_sync_obj_flush,
278 .sync_obj_unref = vmw_sync_obj_unref,
Thomas Hellstromeffe1102010-01-13 22:28:39 +0100279 .sync_obj_ref = vmw_sync_obj_ref,
Thomas Hellstrom476d51d2010-01-13 22:28:41 +0100280 .move_notify = vmw_move_notify,
Jerome Glisse96bf8b82010-04-09 14:39:26 +0200281 .swap_notify = vmw_swap_notify,
282 .fault_reserve_notify = &vmw_ttm_fault_reserve_notify,
283 .io_mem_reserve = &vmw_ttm_io_mem_reserve,
284 .io_mem_free = &vmw_ttm_io_mem_free,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000285};